通过 pytest 测试类遗嘱时如何防止 PytestCollectionWarning

nat*_*er1 14 python pytest

更新到更一般的情况: 如何在通过 pytest 测试类遗嘱时防止 PytestCollectionWarning?testament.py 的简单示例:

class Testament():
    def __init__(self, name):
        self.name = name

    def check(self):
        return True
Run Code Online (Sandbox Code Playgroud)

和 test_testament.py

from testament.testament import Testament

def test_send():
    testament = Testament("Paul")

    assert testament.check()
Run Code Online (Sandbox Code Playgroud)

当使用 pytest 运行时,这会创建一个 PytestCollectionWarning。有没有办法在不关闭所有警告的情况下抑制导入模块的此警告?

Bru*_*ira 26

您可以__test__ = False在 pytest 应忽略的类中设置属性:

class Testament:
    __test__ = False
Run Code Online (Sandbox Code Playgroud)