更改标准(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_atest_b。您可以通过使用 --deselect tests/foobar/test_foobar_01.py::test_a 调用 pytest,运行 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
...

由于收集器只对目录起作用,如果您两次指定单个测试文件,无论是否指定 --keep-duplicatespytest 仍会将其收集两次。示例:

pytest test_a.py test_a.py

...
collected 2 items
...

更改目录递归

您可以在 ini 文件中设置 norecursedirs 选项,例如在您的项目根目录的 pytest.ini 中:

# content of pytest.ini
[pytest]
norecursedirs = .svn _build tmp*

这将告诉 pytest 不递归进入典型的 subversion 或 sphinx-build 目录,或任何以 tmp 为前缀的目录。

更改命名约定

您可以通过在配置文件中设置 python_filespython_classespython_functions 来配置不同的命名约定。下面是一个示例:

# content of pytest.ini
# 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-8.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
configfile: pytest.ini
collected 2 items

<Dir pythoncollection.rst-208>
  <Module check_myapp.py>
    <Class CheckMyApp>
      <Function simple_check>
      <Function complex_check>

======================== 2 tests collected in 0.12s ========================

您可以通过在模式之间添加空格来检查多个全局模式:

# Example 2: have pytest look for files with "test" and "example"
# content of pytest.ini
[pytest]
python_files = test_*.py example_*.py

注意

python_functionspython_classes 选项对 unittest.TestCase 测试发现没有影响,因为 pytest 将测试用例方法的发现委托给 unittest 代码。

将命令行参数解释为 Python 包

您可以使用 --pyargs 选项使 pytest 尝试将参数解释为 Python 包名,推导出其文件系统路径,然后运行测试。例如,如果您安装了 unittest2,您可以输入:

pytest --pyargs unittest2.test.test_skipping -q

这将运行相应的测试模块。与其他选项一样,通过 ini 文件和 addopts 选项,您可以更永久地进行此更改:

# content of pytest.ini
[pytest]
addopts = --pyargs

现在,简单地调用 pytest NAME 将检查 NAME 是否作为可导入的包/模块存在,否则将其视为文件系统路径。

找出收集了什么

您可以通过以下方式查看收集树而不运行测试:

. $ pytest --collect-only pythoncollection.py
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
configfile: pytest.ini
collected 3 items

<Dir pythoncollection.rst-208>
  <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.ini
[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-8.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
configfile: pytest.ini
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__ 属性。