
本文旨在解决 pytest 5.x+ 版本中 `pytest.config` 移除后,如何通过命令行参数控制特定装饰器标记的测试运行或跳过的问题。我们将介绍一种优雅的解决方案,即利用 pytest 的自定义标记(custom markers)功能结合 `-m` 命令行选项,实现对测试执行流程的精细化管理,同时最大限度地兼容旧版装饰器语法,避免大量代码重构。
在 Pytest 4.x 及更早版本中,开发者通常会使用 pytest.config.getoption 来获取命令行参数,并结合 pytest.mark.skipif 装饰器,实现基于特定命令行标志来条件性地跳过或运行测试。例如,以下代码片段展示了如何定义一个 integration 装饰器,仅当 --integration 命令行标志存在时才运行被标记的集成测试:
# common.py (Pytest 4.x 示例)
import pytest
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_part():
assert 2 == 2然而,随着 Pytest 升级到 5.x+ 版本,pytest.config 对象被移除,上述代码将导致 AttributeError: module 'pytest' has no attribute 'config' 错误。这给那些依赖此机制管理测试执行流程的项目带来了迁移挑战,尤其是在存在大量使用此类装饰器的测试时。
Pytest 5.x+ 提供了一个更强大、更标准化的方式来管理测试的元数据和执行流程——自定义标记(Custom Markers)。通过自定义标记,我们可以实现与旧版 pytest.config 方案相同的功能,甚至更加灵活,并且能够完美兼容现有的装饰器语法。
核心思路是:
首先,在项目的根目录下创建一个 pytest.ini 文件(如果尚未存在),并在其中声明你的自定义标记。例如,我们要定义一个名为 integration 的标记:
# pytest.ini
[pytest]
markers =
integration: mark a test as an integration test.这里,markers 部分列出了所有自定义标记,并可以为其提供一个简短的描述。
接下来,修改你的 integration 装饰器定义,使其直接使用 pytest.mark.integration。这样,你现有的所有被 @integration 装饰的测试代码都无需改动。
# common.py (Pytest 5.x+ 兼容)
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_part():
"""这是另一个集成测试。"""
assert 2 == 2
def test_regular_unit_test():
"""这是一个普通的单元测试,没有集成标记。"""
assert True一旦定义了自定义标记并将其应用到测试中,你就可以使用 Pytest 的 -m 命令行选项来选择性地运行或跳过这些测试。
运行所有测试: 不带任何 -m 选项时,Pytest 会运行所有发现的测试。
$ pytest -v ============================= test session starts ============================== platform linux -- Python 3.11.6, pytest-7.2.2, pluggy-1.0.0 rootdir: /path/to/your/project, configfile: pytest.ini collected 3 items test_something.py::test_my_integration_feature PASSED [ 33%] test_something.py::test_another_integration_part PASSED [ 66%] test_something.py::test_regular_unit_test PASSED [100%] ============================== 3 passed in 0.00s ===============================
仅运行集成测试: 使用 -m integration 选项,Pytest 将只运行带有 integration 标记的测试。
$ pytest -v -m integration ============================= test session starts ============================== platform linux -- Python 3.11.6, pytest-7.2.2, pluggy-1.0.0 rootdir: /path/to/your/project, configfile: pytest.ini collected 3 items / 1 deselected / 2 selected test_something.py::test_my_integration_feature PASSED [ 50%] test_something.py::test_another_integration_part PASSED [100%] ======================== 2 passed, 1 deselected in 0.00s =======================
仅运行非集成测试(即跳过集成测试): 使用 -m 'not integration' 选项,Pytest 将只运行不带 integration 标记的测试。注意,not integration 表达式需要用引号包裹起来,以避免 shell 解析问题。
$ pytest -v -m 'not integration' ============================= test session starts ============================== platform linux -- Python 3.11.6, pytest-7.2.2, pluggy-1.0.0 rootdir: /path/to/your/project, configfile: pytest.ini collected 3 items / 2 deselected / 1 selected test_something.py::test_regular_unit_test PASSED [100%] ======================== 1 passed, 2 deselected in 0.00s =======================
在 Pytest 5.x+ 版本中,面对 pytest.config 的移除,通过利用自定义标记和 -m 命令行选项,我们能够优雅地实现对测试执行的精细化控制。这种方法不仅解决了旧版代码的兼容性问题,还提供了一个更符合 Pytest 设计哲学且功能强大的测试管理机制。通过合理地定义和使用自定义标记,开发者可以轻松地管理不同类型的测试,提高测试套件的效率和可维护性。
以上就是Pytest 5.x+ 升级:利用自定义标记和命令行参数高效管理测试执行的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号