我有一个测试文件,例如: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/