我有一个文件,所有通过的测试都存储在一个测试文件中。该文件包含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')