缺少测试覆盖率 pytest python 类

时间:2021-02-10 22:19:34

标签: python python-3.x pytest code-coverage pytest-cov

我正在使用 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% - 我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

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

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

相关问题