Python 如何在通过pytest测试类测试时防止PytestCollectionWarning

Python 如何在通过pytest测试类测试时防止PytestCollectionWarning,python,pytest,Python,Pytest,更新到更一般的情况: 在通过pytest测试类测试时,如何防止PytestCollectionWarning?testant.py的简单示例: class Testament(): def __init__(self, name): self.name = name def check(self): return True 还有test_testament.py from testament.testament import Testament

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

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

    def check(self):
        return True
还有test_testament.py

from testament.testament import Testament

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

    assert testament.check()

这将在使用pytest运行时创建PytestCollectionWarning。有没有一种方法可以在不关闭所有警告的情况下抑制导入模块的此警告?

您可以在配置文件中定义选项,将默认的
Test*
更改为例如
*Tests
您可以使用以下标志运行pytest:

-W ignore::pytest.PytestCollectionWarning
根据以下说明,这只是python的“正常”警告过滤器:

-W命令行选项和filterwarnings ini选项都基于Python自己的-W选项和warnings.simplefilter


您可以在pytest应忽略的类中设置
\uuuuuuu test\uuuu=False
属性:

class Testament:
    __test__ = False

当它不是测试用例时,不要使用
Test…
。@quamrana我知道是这个名称引起了问题。但是直到现在我还没有找到更好的名字。我使用类似于[TestCase.Zero,Status.UNDEFINED]的东西来定义测试的内容和预期的结果。这不是一个大问题,只是一个警告,没有什么阻碍测试。它只会使输出变得杂乱无章。我想知道,是否有可能告诉pytest在特定情况下不显示收集警告(比如我可以告诉它跳过某个测试或文件或文件夹)。可能重复?@Suzana我认为这不是同一个问题。我这里的问题与unittest.TestCase的子类化无关(至少据我所知,我根本不使用unittest)。我为pytest创建了一个功能请求,但似乎不可能:目前,唯一的解决方案似乎是某种变通方法(如@Suzana和@eric的回答中所述),这个解决方案的问题是,它还可能关闭一些有用的警告-我不想这样做。它类似于捕获BaseException——一开始很容易,但在你最不经意的时候可能会咬到你。这是最简单的方法。谢谢!