Skip to content

Commit 143c991

Browse files
committed
WIP Use pytest-dependency and a collect hook to chain tests
FIXME: dependency parameter "host"
1 parent 862e0bf commit 143c991

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

tests/install/conftest.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,45 @@
55

66
from lib.commands import local_cmd, scp, ssh
77

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

tests/install/test_install.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
CACHE_IMPORTED_VM = False
1515
assert CACHE_IMPORTED_VM in [True, False]
1616

17+
@pytest.mark.dependency()
1718
class TestInstallNested:
1819
@pytest.mark.vm_definitions(
1920
dict(name="vm 1",
@@ -120,6 +121,7 @@ def test_install_nested_821_uefi(self, iso_remaster, create_vms):
120121
host_vm.export("test_install_nested_821_uefi-vm1.xva", "zstd",
121122
use_cache=CACHE_IMPORTED_VM)
122123

124+
@pytest.mark.dependency(depends=["TestInstallNested::test_install_nested_821_uefi"])
123125
@pytest.mark.vm_definitions(
124126
dict(name="vm 1",
125127
image="test_install_nested_821_uefi-vm1.xva"
@@ -223,6 +225,7 @@ def test_firstboot_nested_821_uefi(self, create_vms):
223225
host_vm.export("test_firstboot_nested_821_uefi-vm1.xva", "zstd",
224226
use_cache=CACHE_IMPORTED_VM)
225227

228+
@pytest.mark.dependency(depends=["TestInstallNested::test_firstboot_nested_821_uefi"])
226229
@pytest.mark.vm_definitions(
227230
dict(name="vm 1",
228231
image="test_firstboot_nested_821_uefi-vm1.xva"

0 commit comments

Comments
 (0)