Skip to content

Commit

Permalink
WIP Use pytest-dependency and a collect hook to chain tests
Browse files Browse the repository at this point in the history
FIXME: dependency parameter "host"
  • Loading branch information
ydirson committed May 28, 2024
1 parent 862e0bf commit 143c991
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tests/install/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,45 @@

from lib.commands import local_cmd, scp, ssh

# auto-collect tests that are dependencies of selected ones
# adapted from RKrahl/pytest-dependency#56
# #@pytest.hookimpl(trylast=True)
def collect_dependencies(config, item, items):
dependencies = list()
markers = item.own_markers
for marker in markers:
depends = marker.kwargs.get('depends')
scope = marker.kwargs.get('scope')
if marker.name == 'dependency' and depends:
for depend in depends:
if scope == 'session' or scope == 'package':
depend_module, depend_func = depend.split("::", 1)
depend_path = py.path.local(Path(config.rootdir) / Path(depend_module))
depend_parent = Module.from_parent(item.parent, fspath=depend_path)
depend_nodeid = depend
else:
if "::" in depend:
depend_func = depend.split("::")[-1]
else:
depend_func = depend
depend_parent = item.parent
depend_nodeid = '{}::{}'.format(depend_parent.nodeid, depend_func)
# assert depend_nodeid == depend_nodeid2
dependencies.append((depend_func, depend_nodeid, depend_parent))

for depend_func, depend_nodeid, depend_parent in dependencies:
list_of_items_nodeid = [item_i.nodeid for item_i in items]
if depend_nodeid not in list_of_items_nodeid:
item_to_add = pytest.Function.from_parent(name=depend_func, parent=depend_parent)
items.insert(0, item_to_add)
# recursive look for dependencies into item_to_add
collect_dependencies(config, item_to_add, items)
return

def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item]) -> None:
for item in items:
collect_dependencies(config, item, items)

@pytest.fixture(scope='function')
def answerfile(request):
markers = request.node.get_closest_marker("answerfile")
Expand Down
3 changes: 3 additions & 0 deletions tests/install/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
CACHE_IMPORTED_VM = False
assert CACHE_IMPORTED_VM in [True, False]

@pytest.mark.dependency()
class TestInstallNested:
@pytest.mark.vm_definitions(
dict(name="vm 1",
Expand Down Expand Up @@ -120,6 +121,7 @@ def test_install_nested_821_uefi(self, iso_remaster, create_vms):
host_vm.export("test_install_nested_821_uefi-vm1.xva", "zstd",
use_cache=CACHE_IMPORTED_VM)

@pytest.mark.dependency(depends=["TestInstallNested::test_install_nested_821_uefi"])
@pytest.mark.vm_definitions(
dict(name="vm 1",
image="test_install_nested_821_uefi-vm1.xva"
Expand Down Expand Up @@ -223,6 +225,7 @@ def test_firstboot_nested_821_uefi(self, create_vms):
host_vm.export("test_firstboot_nested_821_uefi-vm1.xva", "zstd",
use_cache=CACHE_IMPORTED_VM)

@pytest.mark.dependency(depends=["TestInstallNested::test_firstboot_nested_821_uefi"])
@pytest.mark.vm_definitions(
dict(name="vm 1",
image="test_firstboot_nested_821_uefi-vm1.xva"
Expand Down

0 comments on commit 143c991

Please sign in to comment.