gpt4 book ai didi

python - 使用一组标记运行测试,这些标记是根据命令行参数的值选择的

转载 作者:行者123 更新时间:2023-12-04 07:51:32 24 4
gpt4 key购买 nike

我有以下测试:

@pytest.mark.hardware
@pytest.mark.feature1
@pytest.mark.feature2
def test_device1():
pass

@pytest.mark.hardware
@pytest.mark.feature1
def test_device2():
pass

@pytest.mark.hardware
@pytest.mark.feature2
def test_device3():
pass

目标:如果我在命令行参数上指定:pytest --device device1 ,我希望只运行标记为 feature1、feature2、hardware 的测试。类似地:参数 device2 只会调用标记为 hardware 和 feature1 等的测试。如果没有参数,将运行没有标记的测试。

在 conftest.py 中我有:

def pytest_addoption(parser):
group = parser.getgroup("filter")
group.addoption(
"--device",
action="store",
help="specify the device",
)

我发现 pytest_collection_modifyitems 可能有帮助,但我不知道如何根据命令行参数的值选择要运行的标记列表。在此先感谢您的帮助。

我试过了,但是没用:

def pytest_collection_modifyitems(config, items):
if config.getoption("--device") == 'device2':
for item in items:
item.add_marker(pytest.mark.hardware)
item.add_marker(pytest.mark.feature1)

最佳答案

您必须根据device 选项条件过滤items 列表。实现例:

def marker_names(item):
return set(m.name for m in item.iter_markers())


def pytest_collection_modifyitems(config, items):
device_markers = {
"device1": {"feature1", "feature2", "hardware"},
"device2": {"feature1", "hardware"},
}

device_option = config.getoption("--device")

if device_option is None:
items[:] = [item for item in items if not list(item.iter_markers())]
else:
allowed_markers = device_markers[device_option]
items[:] = [item for item in items if marker_names(item) == allowed_markers]

您也可以跳过测试而不是将它们排除在外。实现例:

def marker_names(item):
return set(m.name for m in item.iter_markers())


def pytest_collection_modifyitems(config, items):
device_markers = {
"device1": {"feature1", "feature2", "hardware"},
"device2": {"feature1", "hardware"},
}

device_option = config.getoption("--device")

if device_option is None:
for item in items:
if list(item.iter_markers()):
item.add_marker(pytest.mark.skip("has no markers"))
else:
allowed_markers = device_markers[device_option]
for item in items:
if marker_names(item) != allowed_markers:
item.add_marker(pytest.mark.skip(f"has some markers missing for {device_option}"))

关于python - 使用一组标记运行测试,这些标记是根据命令行参数的值选择的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66949165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com