如何在Pytest中将参数化测试正确分组?

时间:2019-07-08 12:27:31

标签: python pytest

我目前正在使用以下Pytest挂钩组合为特定模块自定义节点ID:

# In test_module.py
def pytest_generate_tests(metafunc):
    if not is_relevant_metafunc(metafunc):
        return
    args = []
    fids = []
    for test_class, test_name, test_args in collect_external_tests():
        args.append(test_args)
        fids.append("Tests.%s.%s" % (test_class, test_name))

    metafunc.parametrize(["arg1", "arg2"], args, ids=fids)

# In conftest.py
def pytest_collection_modifyitems(session, config, items):
    for item in items:
        if not is_relevant_item(item):
            continue
        item._nodeid = item.callspec.id
        item._location = tuple(list(item.location)[:-1] + [item.callspec.id])

这样做的平均原因是,我希望将参数化测试分散到输出JUnit XML文件的不同部分(“类”)中,然后可以将其汇总并单独显示。

默认情况下,pytest创建以下层次结构:

root
 |_ test_module
    |_ test_module.test_func[param_a_1]
    |_ test_module.test_func[param_b_2]
    |_ ...
    |_ test_module.test_func[param_z_36]

我想要具有类似于以下层次结构的内容:

root
 |_ test_module
    |_ a
       |_ test_module.test_func[param_a_1]
       |_ ...
       |_ test_module.test_func[param_a_25]
    |_ b
       |_ test_module.test_func[param_b_1]
       |_ ...
       |_ test_module.test_func[param_b_12]
    |_ ...

有更好的方法吗?

0 个答案:

没有答案
相关问题