更改标准 (Python) 测试发现¶
在测试收集期间忽略路径¶
您可以在收集期间通过在命令行中传递 --ignore=path 选项,轻松忽略某些测试目录和模块。pytest 允许使用多个 --ignore 选项。示例
tests/
|-- example
| |-- test_example_01.py
| |-- test_example_02.py
| '-- test_example_03.py
|-- foobar
| |-- test_foobar_01.py
| |-- test_foobar_02.py
| '-- test_foobar_03.py
'-- hello
'-- world
|-- test_world_01.py
|-- test_world_02.py
'-- test_world_03.py
现在,如果您使用 --ignore=tests/foobar/test_foobar_03.py --ignore=tests/hello/ 调用 pytest,您会发现 pytest 只收集与指定模式不匹配的测试模块
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y
rootdir: $REGENDOC_TMPDIR, inifile:
collected 5 items
tests/example/test_example_01.py . [ 20%]
tests/example/test_example_02.py . [ 40%]
tests/example/test_example_03.py . [ 60%]
tests/foobar/test_foobar_01.py . [ 80%]
tests/foobar/test_foobar_02.py . [100%]
========================= 5 passed in 0.02 seconds =========================
--ignore-glob 选项允许根据 Unix shell 风格的通配符忽略测试文件路径。如果您想排除以 _01.py 结尾的测试模块,请使用 --ignore-glob='*_01.py' 执行 pytest。
在测试收集期间取消选择测试¶
在收集期间,可以通过传递 --deselect=item 选项来单独取消选择测试。例如,假设 tests/foobar/test_foobar_01.py 包含 test_a 和 test_b。您可以通过调用 pytest 并带上 --deselect tests/foobar/test_foobar_01.py::test_a 来运行 tests/ 中的所有测试,除了 tests/foobar/test_foobar_01.py::test_a。pytest 允许使用多个 --deselect 选项。
保留命令行中指定的重复路径¶
pytest 的默认行为是忽略命令行中指定的重复路径。示例
pytest path_a path_a
...
collected 1 item
...
只收集一次测试。
要收集重复的测试,请在命令行中使用 --keep-duplicates 选项。示例
pytest --keep-duplicates path_a path_a
...
collected 2 items
...
更改目录递归¶
您可以在配置文件中设置 norecursedirs 选项
# content of pytest.toml
[pytest]
norecursedirs = [".svn", "_build", "tmp*"]
这会告诉 pytest 不要递归到典型的 subversion 或 sphinx-build 目录中,也不要递归到任何以 tmp 为前缀的目录中。
更改命名约定¶
您可以通过在 配置文件 中设置 python_files、python_classes 和 python_functions 来配置不同的命名约定。这是一个示例
# content of pytest.toml
# Example 1: have pytest look for "check" instead of "test"
[pytest]
python_files = ["check_*.py"]
python_classes = ["Check"]
python_functions = ["*_check"]
这将使 pytest 在匹配 check_* .py 通配符模式的文件中查找测试,在类中查找 Check 前缀,以及匹配 *_check 的函数和方法。例如,如果我们有
# content of check_myapp.py
class CheckMyApp:
def simple_check(self):
pass
def complex_check(self):
pass
测试收集将如下所示
$ pytest --collect-only
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-9.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
configfile: pytest.toml
collected 2 items
<Dir pythoncollection.rst-212>
<Module check_myapp.py>
<Class CheckMyApp>
<Function simple_check>
<Function complex_check>
======================== 2 tests collected in 0.12s ========================
您可以通过在模式之间添加空格来检查多个通配符模式
# content of pytest.toml
# Example 2: have pytest look for files with "test" and "example"
[pytest]
python_files = ["test_*.py", "example_*.py"]
注意
python_functions 和 python_classes 选项对 unittest.TestCase 测试发现没有影响,因为 pytest 将测试用例方法的发现委托给 unittest 代码。
将命令行参数解释为 Python 包¶
您可以使用 --pyargs 选项,让 pytest 尝试将参数解释为 Python 包名,推导出其文件系统路径,然后运行测试。例如,如果您安装了 unittest2,您可以输入
pytest --pyargs unittest2.test.test_skipping -q
这将运行相应的测试模块。与其他选项一样,通过配置文件和 addopts 选项,您可以更永久地进行此更改
# content of pytest.toml
[pytest]
addopts = ["--pyargs"]
现在,简单调用 pytest NAME 将检查 NAME 是否作为可导入的包/模块存在,否则将其视为文件系统路径。
找出收集了什么¶
您可以始终在不运行测试的情况下查看收集树,如下所示
. $ pytest --collect-only pythoncollection.py
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-9.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
configfile: pytest.toml
collected 3 items
<Dir pythoncollection.rst-212>
<Dir CWD>
<Module pythoncollection.py>
<Function test_function>
<Class TestClass>
<Function test_method>
<Function test_anothermethod>
======================== 3 tests collected in 0.12s ========================
自定义测试收集¶
您可以轻松地指示 pytest 从每个 Python 文件中发现测试
# content of pytest.toml
[pytest]
python_files = ["*.py"]
然而,许多项目会有一个 setup.py 文件,他们不希望它被导入。此外,可能有些文件只能由特定 Python 版本导入。对于这些情况,您可以通过在 conftest.py 文件中列出要忽略的文件来动态定义
# content of conftest.py
import sys
collect_ignore = ["setup.py"]
if sys.version_info[0] > 2:
collect_ignore.append("pkg/module_py2.py")
然后如果你有一个这样的模块文件
# content of pkg/module_py2.py
def test_only_on_python2():
try:
assert 0
except Exception, e:
pass
以及一个这样的 setup.py 虚拟文件
# content of setup.py
0 / 0 # will raise exception if imported
如果您使用 Python 2 解释器运行,您会发现一个测试,并且会忽略 setup.py 文件
#$ pytest --collect-only
====== test session starts ======
platform linux2 -- Python 2.7.10, pytest-2.9.1, py-1.4.31, pluggy-0.3.1
rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini
collected 1 items
<Module 'pkg/module_py2.py'>
<Function 'test_only_on_python2'>
====== 1 tests found in 0.04 seconds ======
如果您使用 Python 3 解释器运行,则该测试和 setup.py 文件都将被忽略
$ pytest --collect-only
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-9.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
configfile: pytest.toml
collected 0 items
======================= no tests collected in 0.12s ========================
也可以通过将模式添加到 collect_ignore_glob 来忽略基于 Unix shell 风格通配符的文件。
下面的 conftest.py 示例在 Python 3 解释器执行时忽略文件 setup.py,并额外忽略所有以 *_py2.py 结尾的文件
# content of conftest.py
import sys
collect_ignore = ["setup.py"]
if sys.version_info[0] > 2:
collect_ignore_glob = ["*_py2.py"]
自 Pytest 2.6 起,用户可以通过将布尔值 __test__ 属性设置为 False 来阻止 pytest 发现以 Test 开头的类。
# Will not be discovered as a test
class TestClass:
__test__ = False
注意
如果您正在使用抽象测试类,并且希望避免手动为子类设置 __test__ 属性,您可以使用混合类自动处理此问题。例如
# Mixin to handle abstract test classes
class NotATest:
def __init_subclass__(cls):
cls.__test__ = NotATest not in cls.__bases__
# Abstract test class
class AbstractTest(NotATest):
pass
# Subclass that will be collected as a test
class RealTest(AbstractTest):
def test_example(self):
assert 1 + 1 == 2
这种方法确保抽象测试类的子类被自动收集,而无需显式设置 __test__ 属性。