
`pytest` 5.x+ 版本移除了 `pytest.config`,导致旧版中通过命令行标志动态跳过或运行特定测试的方法不再适用。本文旨在提供一种现代且兼容的解决方案,即利用 `pytest` 的自定义标记(markers)结合 `-m` 命令行选项,实现对装饰器修饰的测试进行精确的条件性执行控制,确保升级过程的平滑过渡,并保持代码的简洁性。
在 pytest 从 4.x 版本升级到 5.x+ 版本时,开发者经常会遇到一个挑战:旧版本中用于通过命令行标志动态控制测试运行或跳过的 pytest.config 对象已被移除。这使得依赖 pytest.config.getoption('--flag', False) 的 pytest.mark.skipif 装饰器不再有效,导致 AttributeError: module 'pytest' has no attribute 'config' 错误。为了在不大量修改现有测试装饰器语法的前提下,实现相同的功能,我们需要采用 pytest 5.x+ 版本推荐的自定义标记(Custom Markers)机制。
在 pytest 4.x 版本中,常见的做法是定义一个跳过装饰器,该装饰器根据命令行选项来决定是否跳过测试。例如,一个用于集成测试的装饰器可能如下所示:
# common.py
import pytest
# 如果命令行未提供 --integration 标志,则跳过
integration = pytest.mark.skipif(
not pytest.config.getoption('--integration', False),
reason="需要 --integration 标志才能运行集成测试"
)
# test_something.py
from .common import integration
@integration
def test_my_integration_feature():
assert 1 == 1
@integration
def test_another_integration_feature():
assert 2 == 2
def test_regular_feature():
assert True在 pytest 5.x+ 中,由于 pytest.config 的移除,上述 common.py 中的 integration 装饰器将不再工作。
pytest 5.x+ 推荐使用自定义标记来对测试进行分类,并通过 -m 命令行选项来选择或排除特定标记的测试。这种方法更加灵活且符合 pytest 的设计哲学。
首先,我们需要重新定义 integration 装饰器。不再使用 pytest.mark.skipif 结合 pytest.config.getoption,而是直接创建一个 pytest.mark 实例作为我们的自定义标记。
# common.py
import pytest
# 定义一个名为 'integration' 的自定义标记
integration = pytest.mark.integration
# test_something.py (保持与旧版类似的装饰器使用方式)
from .common import integration
@integration
def test_my_integration_feature():
"""这是一个集成测试."""
assert 1 == 1
@integration
def test_another_integration_feature():
"""这是另一个集成测试."""
assert 2 == 2
def test_regular_feature():
"""这是一个常规的单元测试."""
assert True可以看到,test_something.py 中的测试函数装饰器语法保持不变,这对于平滑升级大量现有测试代码至关重要。
为了避免 PytestUnknownMarkWarning 警告并提高测试的可维护性,强烈建议在 pytest.ini 文件中注册所有自定义标记。
在项目的根目录下创建或修改 pytest.ini 文件,并添加 markers 部分:
# pytest.ini
[pytest]
markers =
integration: mark a test as an integration test.
# 您可以根据需要添加其他自定义标记及其描述,例如:
# slow: marks tests as slow (deselect with '-m "not slow"')一旦定义并注册了自定义标记,我们就可以使用 pytest 的 -m 命令行选项来灵活地运行或跳过带有特定标记的测试。
运行所有测试 (包括集成和非集成测试):
pytest -v
此命令将执行所有收集到的测试,无论它们是否被 @integration 标记。
只运行集成测试:
pytest -v -m integration
此命令会告诉 pytest 只运行那些被 @integration 标记的测试。其他未被此标记修饰的测试将被跳过(deselected)。
示例输出 (摘要):
========================================= test session starts ========================================= ... collected 3 items / 1 deselected / 2 selected test_something.py::test_my_integration_feature PASSED test_something.py::test_another_integration_feature PASSED =================================== 2 passed, 1 deselected in 0.00s ===================================
只运行非集成测试:
pytest -v -m 'not integration'
使用 not 关键字可以排除带有特定标记的测试。此命令将运行所有未被 @integration 标记的测试。
示例输出 (摘要):
========================================= test session starts ========================================= ... collected 3 items / 2 deselected / 1 selected test_something.py::test_regular_feature PASSED =================================== 1 passed, 2 deselected in 0.00s ===================================
通过上述方法,我们成功地在 pytest 5.x+ 版本中实现了与旧版 pytest.config 相同甚至更强大的条件测试执行功能。
采用自定义标记是 pytest 5.x+ 中处理此类需求的标准且推荐方式,它使得测试组织更加清晰,执行控制更加灵活。
以上就是pytest 5.x+ 升级:利用自定义标记灵活控制测试的运行与跳过的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号