pytest_collection_modifyitem
在收集完用例后执行,通过此hooks,在conftest.p
y中可以实现对用例的过滤和重新排序
- 根据用例标记和执行参数标记用例是否跳过( --headless运行时,并且标记only_headed的用例跳过)
def pytest_collection_modifyitems(config, items):'''skip the only_headed test case, if run in headless mode'''only_headed = config.getoption("--headed")skip_headed_parameters = pytest.mark.skip(reason="only run in headed mode")for item in items: # only skip the test which mark.only_headedif not only_headed and "only_headed" in item.keywords:item.add_marker(skip_headed_parameters)
- 根据用例动态的批量添加标记
def pytest_collection_modifyitems(items):for item in items:if "interface" in item.nodeid: #用例nodeid中包含interface时标记interfaceitem.add_marker(pytest.mark.interface)elif "event" in item.nodeid: #用例nodeid中包含event时标记eventitem.add_marker(pytest.mark.event)