python-3.x - 如何检查哪个pytest标记是运行的测试

标签 python-3.x pytest

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

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

pipenv run pytest -m "integration"

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

我还有一个全局变量配置如下,并在所有方法中使用这个全局值 DATA
DATA = get_my_data()

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

数据 = get_smoke_data()

问题是在运行测试用例时,我无法拆分此测试用例正在调用哪个标记。即用于烟雾或集成。我怎样才能在全局范围内获得这些信息?

以前我知道有一个叫做 Mark info 的东西,例如: from _pytest.mark import MarkInfo 但现在已经删除了。这仅在每种方法中可用 我怎样才能在全局范围内获得它

最佳答案

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

这是您要找的东西吗?

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
pytest -vvv -s test_marker.py -m smoke

输出:
test_marker.py::TestSmokeIntegration::test_for_smoke_integration markers used:  smoke
PASSED
pytest -vvv -s test_marker.py -m integration

输出:
test_marker.py::TestSmokeIntegration::test_for_smoke_integration marker used:  integration
PASSED

关于python-3.x - 如何检查哪个pytest标记是运行的测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61609496/

相关文章:

python - reshape 数据框并对每行应用计算

python - 不同案例的pytest参数化 fixture

Pytest:如何对测试失败采取行动?

python - 我需要 Python 中的数据库清理器吗?

python - 如何使用Python的SDK在Firestore上编写位置、引用和时间戳数据类型?

python - 如果一个对象没有 `__dict__` ,它的类必须有一个 `__slots__` 属性吗?

python-3.x - numpy 实现自定义 RNG

python - 如何确保只有在 pytest 中明确要求时才运行带有标记的测试?

python - 为什么pytest调用重复方法

python - python3 中的可选 yield 或 return。如何?