更改标准(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
现在,如果你在调用 pytest 时加上 --ignore=tests/foobar/test_foobar_03.py --ignore=tests/hello/,你会发现 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-215>
<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-215>
<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 类来自动处理这个问题。例如:
# 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__ 属性。