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

Chr*_*ers 4 python pytest

我正在尝试通过pytest通过的testfixture 进行测试,但它一直在尝试收集不是测试的内容:

======================================================= 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
Run Code Online (Sandbox Code Playgroud)

我怎样才能让pytest只收集子类unittest.TestCase的Test *类?

wim*_*wim 5

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

默认值是这样的

[pytest]
python_files=test_*.py *_test.py
python_classes=Test
python_functions=test
Run Code Online (Sandbox Code Playgroud)

也许您可以通过覆盖它来自定义它不收集类:

# pytest.ini (or in tox.ini, or in setup.cfg)
[pytest]  # if using setup.cfg, this section name should instead be [tool:pytest]
python_classes=NoThanks
Run Code Online (Sandbox Code Playgroud)

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

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


Dav*_*ger 5

我在Pytest 2.6发行说明中发现了这一点:

__test__在模块,类和函数(包括unittest样式的类)上支持鼻子样式的属性。如果设置为False,则不会收集测试。

这可以帮助如果你的类收集,当你不希望它是,但它“因为它有一个无法收集测试类与不利于__init__构造”的警告。