Skip to content

Commit 1c7193a

Browse files
committed
Use pytest-dependency and a collect hook to chain tests
This is pretty basic, but works well for tests without implicit parameters, and we got rid of most of them already \o/. Signed-off-by: Yann Dirson <[email protected]>
1 parent 1e0ed87 commit 1c7193a

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+
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+
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)