我怎样才能让pytest忽略不属于unittest子类的Test*类?

2024-09-28 01:33:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过pytest获取testfixtures的测试,但它一直试图收集不是测试的内容:

======================================================= pytest-warning summary ========================================================
WC1 /Users/chris/vcs/git/testfixtures/testfixtures/tests/test_comparison.py cannot collect test class 'TestClassA' because it has a __init__ constructor
WC1 /Users/chris/vcs/git/testfixtures/testfixtures/tests/test_components.py cannot collect test class 'TestComponents' because it has a __init__ constructor
WC1 /Users/chris/vcs/git/testfixtures/testfixtures/tests/test_datetime.py cannot collect test class 'TestTZInfo' because it has a __new__ constructor
WC1 /Users/chris/vcs/git/testfixtures/testfixtures/tests/test_datetime.py cannot collect test class 'TestTZ2Info' because it has a __new__ constructor
cannot collect test class 'TestContainer' because it has a __init__ constructor
cannot collect test class 'TestTZInfo' because it has a __new__ constructor

如何让pytest只收集子类的Test*类unittest.TestCase?在


Tags: testgittestsitvcsusersclasschris
2条回答

我在Pytest 2.6 release notes中找到了这个:

support nose-style __test__ attribute on modules, classes and functions, including unittest-style Classes. If set to False, the test will not be collected.

如果您的类在您不希望被收集的时候被收集,这会有所帮助,但对于“cannot collect test class because a __init__构造函数”警告没有帮助。在

Pytest使用glob样式的名称模式来收集测试,并且可以通过配置文件中的选项python_filespython_classes和{}自定义发现。在

我认为违约是这样的:

[pytest]
python_files=test_*.py
python_classes=Test*
python_functions=test_*

也许您可以通过重写以下内容将其自定义为不收集类:

^{2}$

注意:这里不限于一种模式,您可以指定它们的列表。在

无论此选项如何,仍应收集继承unittest.TestCase的类。这是因为unittest框架本身用于收集这些测试。在

相关问题 更多 >

    热门问题