Python 3.x 如何检查正在为哪个pytest标记运行测试
我有一个测试文件,例如:test_my_api.py 我使用下面的命令调用pytest开始执行,以便仅使用特定标记运行测试Python 3.x 如何检查正在为哪个pytest标记运行测试,python-3.x,pytest,Python 3.x,Pytest,我有一个测试文件,例如:test_my_api.py 我使用下面的命令调用pytest开始执行,以便仅使用特定标记运行测试 pipenv run pytest -m "integration" 现在在我的test_my_api.py文件中,我有多个标记为integration的函数 我还有一个全局变量,配置如下,并在所有方法中使用这个全局值数据 DATA = get_my_data() 现在我有了另一个标记smoke,现在一些测试用例既有smoke标记也有integration标记。对于烟雾
pipenv run pytest -m "integration"
现在在我的test_my_api.py文件中,我有多个标记为integration的函数
我还有一个全局变量,配置如下,并在所有方法中使用这个全局值数据
DATA = get_my_data()
现在我有了另一个标记smoke,现在一些测试用例既有smoke标记也有integration标记。对于烟雾,我需要如下不同的全球数据
数据=获取烟雾数据
问题是在运行测试用例时,我无法拆分调用此测试用例的标记。i、 e表示烟雾或集成。如何在全球范围内获取此信息
以前我知道有一个叫做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
输出:
test_marker.py::TestSmokeIntegration::test_for_smoke_integration markers used: smoke
PASSED
test_marker.py::TestSmokeIntegration::test_for_smoke_integration marker used: integration
PASSED
输出:
test_marker.py::TestSmokeIntegration::test_for_smoke_integration markers used: smoke
PASSED
test_marker.py::TestSmokeIntegration::test_for_smoke_integration marker used: integration
PASSED
如果我理解正确,您试图知道在运行时具有多个标记的测试方法调用哪个标记 这是你要找的东西吗
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
输出:
test_marker.py::TestSmokeIntegration::test_for_smoke_integration markers used: smoke
PASSED
test_marker.py::TestSmokeIntegration::test_for_smoke_integration marker used: integration
PASSED
输出:
test_marker.py::TestSmokeIntegration::test_for_smoke_integration markers used: smoke
PASSED
test_marker.py::TestSmokeIntegration::test_for_smoke_integration marker used: integration
PASSED
我不确定我是否理解您的问题:您所说的这个测试用例的哪个标记被调用是什么意思?你的意思是如果你为测试过滤,测试必须知道使用了哪个过滤器?你能解释一下你想达到什么目的吗?是的,请更正此测试执行的过滤器。也就是说,如果我可以用2个过滤器标记一个测试用例,并且在运行时我将只指定一个过滤器,也就是说,在运行时我想知道这个测试用例是针对哪个过滤器执行的,那么您想根据调用它的过滤器来更改测试的行为吗?例如,如果pytest过滤集成一个行为,如果它过滤烟雾,如果在没有过滤器的情况下调用,则是其他行为?在我看来,这似乎是混合了两个概念——过滤和测试配置。我仍然不确定你想要实现什么,也许有更好的方法?我正在试图找出如何获得这个值。这表示我可以使用_pytest.nodes.Node.iter_标记来获取标记名。但我不知道如何使用它,没有一个例子,这是为了获得测试功能的标记,比如说,从夹具或插件。这与您在pytest中使用的过滤器无关。取决于你到底想做什么,但你仍然不知道,这可能对你有帮助,也可能没有帮助。我不确定我是否理解你的问题:你说这个测试用例的哪个标记被调用是什么意思?你的意思是如果你为测试过滤,测试必须知道使用了哪个过滤器?你能解释一下你想达到什么目的吗?是的,请更正此测试执行的过滤器。也就是说,如果我可以用2个过滤器标记一个测试用例,并且在运行时我将只指定一个过滤器,也就是说,在运行时我想知道这个测试用例是针对哪个过滤器执行的,那么您想根据调用它的过滤器来更改测试的行为吗?例如,如果pytest过滤集成一个行为,如果它过滤烟雾,如果在没有过滤器的情况下调用,则是其他行为?在我看来,这似乎是混合了两个概念——过滤和测试配置。我仍然不确定你想要实现什么,也许有更好的方法?我正在试图找出如何获得这个值。这表示我可以使用_pytest.nodes.Node.iter_标记来获取标记名。但我不知道如何使用它,没有一个例子,这是为了获得测试功能的标记,比如说,从夹具或插件。这与您在pytest中使用的过滤器无关。这取决于你到底想做什么,但你仍然不知道,这可能对你有帮助,也可能没有帮助。