pytest在收集挂钩中形成全限定测试名称

时间:2019-03-28 09:19:23

标签: python pytest

我有一个文件,所有通过的测试都存储在一个测试文件中。该文件包含pytest -rp

的输出
PASSED foo.py::TestFoo::()::test_foo
PASSED foo.py::TestFoo::()::test_bar

我要收集所有测试,然后如果测试的完全限定名称等于文件中的任何测试,然后用pytest_collection_modifyitems标记它们。有没有一种方法可以用来以上述格式形成名称?我的用例是在文件中具有已通过测试的列表,并在收集所有测试后用标记标记这些测试。该文件有数千行,因此我不能在命令行中使用pytest -k

def pytest_collection_modifyitems(items):

    locations = set()
    with open('python3_tests.txt', 'a') as f:
        for location in locations:
            locations.add(location.strip())

    for item in items:
        cls_name  = '::'.join(item.location[2].split('.', 1)) # tuple of (file_name, line number, cls_name.function_name)
        if cls_name in locations:
            item.add_marker('py3')

0 个答案:

没有答案