4

我正在使用 pytest --cov 测试我的代码,但我的一个模块的覆盖率为 0%。

该模块有一个类声明,如下所示:

class DataBaseMaker:
    @staticmethod
    def create_database():
        conn = sqlite3.connect("database.db")
        
    def __init__(self):
        self.foo = 'bar'
        

该测试执行以下操作:

def test_create_database():
    DataBaseMaker.create_database()
    assert Path("database.db").is_file()

对此的测试覆盖率为 0% - 我在这里做错了什么?

4

1 回答 1

0

我能够找出问题所在。这不是代码本身,而是我对 pytest 的调用。我做了pytest --cov *projectname*where projectname也是项目所在文件夹的名称。我想我是从 pytest 文档中得到的?没有把握。

解决方案:我跑了pytest --cov .,果然我的课程现在有 100% 的覆盖率。如果有人对这种行为有解释,我很乐意学习。

于 2021-02-12T19:19:13.493 回答