|
5 | 5 |
|
6 | 6 | from lib.commands import local_cmd, scp, ssh
|
7 | 7 |
|
| 8 | +# auto-collect tests that are dependencies of selected ones |
| 9 | +# adapted from RKrahl/pytest-dependency#56 |
| 10 | +def collect_dependencies(config, item, items): |
| 11 | + dependencies = list() |
| 12 | + markers = item.own_markers |
| 13 | + for marker in markers: |
| 14 | + depends = marker.kwargs.get('depends') |
| 15 | + scope = marker.kwargs.get('scope') |
| 16 | + if marker.name == 'dependency' and depends: |
| 17 | + for depend in depends: |
| 18 | + if scope == 'session' or scope == 'package': |
| 19 | + depend_module, depend_func = depend.split("::", 1) |
| 20 | + depend_path = py.path.local(Path(config.rootdir) / Path(depend_module)) |
| 21 | + depend_parent = Module.from_parent(item.parent, fspath=depend_path) |
| 22 | + depend_nodeid = depend |
| 23 | + else: |
| 24 | + if "::" in depend: |
| 25 | + depend_func = depend.split("::")[-1] |
| 26 | + else: |
| 27 | + depend_func = depend |
| 28 | + depend_parent = item.parent |
| 29 | + depend_nodeid = '{}::{}'.format(depend_parent.nodeid, depend_func) |
| 30 | + # assert depend_nodeid == depend_nodeid2 |
| 31 | + dependencies.append((depend_func, depend_nodeid, depend_parent)) |
| 32 | + |
| 33 | + for depend_func, depend_nodeid, depend_parent in dependencies: |
| 34 | + list_of_items_nodeid = [item_i.nodeid for item_i in items] |
| 35 | + if depend_nodeid not in list_of_items_nodeid: |
| 36 | + item_to_add = pytest.Function.from_parent(name=depend_func, parent=depend_parent) |
| 37 | + items.insert(0, item_to_add) |
| 38 | + # recursive look for dependencies into item_to_add |
| 39 | + collect_dependencies(config, item_to_add, items) |
| 40 | + return |
| 41 | + |
| 42 | +# #@pytest.hookimpl(trylast=True) |
| 43 | +def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item]) -> None: |
| 44 | + for item in items: |
| 45 | + collect_dependencies(config, item, items) |
| 46 | + |
8 | 47 | @pytest.fixture(scope='function')
|
9 | 48 | def answerfile(request):
|
10 | 49 | markers = request.node.get_closest_marker("answerfile")
|
|
0 commit comments