如何检查正在运行的测试是哪个 pytest 标记

Nag*_*nan 5 pytest python-3.x

我有一个测试文件,例如:test_my_api.py

我调用 pytest 使用以下命令开始执行,仅使用特定标记运行测试

pipenv run pytest -m "integration"
Run Code Online (Sandbox Code Playgroud)

现在在我的 test_my_api.py 文件中,我有多个标记为“集成”的函数

我还配置了一个全局变量,如下所示,并在所有方法中使用此全局值 DATA

DATA = get_my_data()
Run Code Online (Sandbox Code Playgroud)

现在我有另一个标记称为“smoke”,现在一些测试用例同时具有标记“smoke”和“integration”。对于烟雾,我需要如下不同的全局数据,

数据 = get_smoke_data()

问题是在运行测试用例时,我无法拆分该测试用例被调用的标记。即用于烟雾或集成。我如何在全球范围内获取此信息?

以前我知道有一个叫做 Mark info 的东西,例如: from _pytest.mark import MarkInfo 但现在已删除。这仅在每个方法中可用我如何在全局级别上获得它

Sha*_*eel 9

如果我理解正确,您想知道在运行时具有多个标记的测试方法调用哪个标记?

这是您正在寻找的东西吗?

import pytest

@pytest.fixture
def my_common_fixture(request, pytestconfig):
    markers_arg = pytestconfig.getoption('-m')
    request.cls.marker_name = markers_arg


class TestSmokeIntegration:
    @pytest.mark.smoke
    @pytest.mark.integration
    def test_for_smoke_integration(self, my_common_fixture):
        print("marker used: ", self.marker_name)
        assert True

    def test_somthing_else(my_common_fixture):
        assert True
Run Code Online (Sandbox Code Playgroud)
pytest -vvv -s test_marker.py -m smoke
Run Code Online (Sandbox Code Playgroud)

输出:

test_marker.py::TestSmokeIntegration::test_for_smoke_integration markers used:  smoke
PASSED
Run Code Online (Sandbox Code Playgroud)
pytest -vvv -s test_marker.py -m integration
Run Code Online (Sandbox Code Playgroud)

输出:

test_marker.py::TestSmokeIntegration::test_for_smoke_integration marker used:  integration
PASSED
Run Code Online (Sandbox Code Playgroud)