pytest_collection_modifyitem对用例进行过滤

文章介绍了pytest_collection_modifyitems钩子在pytest中的应用,如何根据配置和用例特性进行过滤(如在headless模式下跳过only_headed用例),以及如何动态地为用例添加interface和event标记。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pytest_collection_modifyitem在收集完用例后执行,通过此hooks,在conftest.py中可以实现对用例的过滤和重新排序

  • 根据用例标记和执行参数标记用例是否跳过( --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_headed
        if 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时标记interface
            item.add_marker(pytest.mark.interface)
        elif "event" in item.nodeid:  #用例nodeid中包含event时标记event
            item.add_marker(pytest.mark.event)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值