3

我有以下测试:

@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)
4

1 回答 1

3

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

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}"))
于 2021-04-06T08:43:33.840 回答