Skip to content

Commit da0ad8e

Browse files
committed
conftest: regroup all pytest hooks together, separated from fixtures
Signed-off-by: Yann Dirson <[email protected]>
1 parent cb42207 commit da0ad8e

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

conftest.py

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
# need to import them in the global conftest.py so that they are recognized as fixtures.
2020
from pkgfixtures import formatted_and_mounted_ext4_disk, sr_disk_wiped
2121

22+
### pytest hooks
23+
2224
def pytest_addoption(parser):
2325
parser.addoption(
2426
"--hosts",
@@ -70,6 +72,42 @@ def pytest_configure(config):
7072
global_config.ignore_ssh_banner = config.getoption('--ignore-ssh-banner')
7173
global_config.ssh_output_max_lines = int(config.getoption('--ssh-output-max-lines'))
7274

75+
def pytest_generate_tests(metafunc):
76+
if "vm_ref" in metafunc.fixturenames:
77+
vms = metafunc.config.getoption("vm")
78+
if not vms:
79+
vms = [None] # no --vm parameter does not mean skip the test, for us, it means use the default
80+
metafunc.parametrize("vm_ref", vms, indirect=True, scope="module")
81+
82+
def pytest_collection_modifyitems(items, config):
83+
# Automatically mark tests based on fixtures they require.
84+
# Check pytest.ini or pytest --markers for marker descriptions.
85+
86+
markable_fixtures = [
87+
'uefi_vm',
88+
'unix_vm',
89+
'windows_vm',
90+
'hostA2',
91+
'hostB1',
92+
'sr_disk',
93+
'sr_disk_4k'
94+
]
95+
96+
for item in items:
97+
fixturenames = getattr(item, 'fixturenames', ())
98+
for fixturename in markable_fixtures:
99+
if fixturename in fixturenames:
100+
item.add_marker(fixturename)
101+
102+
if 'vm_ref' not in fixturenames:
103+
item.add_marker('no_vm')
104+
105+
if item.get_closest_marker('multi_vms'):
106+
# multi_vms implies small_vm
107+
item.add_marker('small_vm')
108+
109+
### fixtures
110+
73111
def setup_host(hostname_or_ip):
74112
pool = Pool(hostname_or_ip)
75113
h = pool.master
@@ -400,37 +438,3 @@ def second_network(pytestconfig, host):
400438
if network_uuid == host.management_network():
401439
pytest.fail("--second-network must NOT be the management network")
402440
return network_uuid
403-
404-
def pytest_generate_tests(metafunc):
405-
if "vm_ref" in metafunc.fixturenames:
406-
vms = metafunc.config.getoption("vm")
407-
if not vms:
408-
vms = [None] # no --vm parameter does not mean skip the test, for us, it means use the default
409-
metafunc.parametrize("vm_ref", vms, indirect=True, scope="module")
410-
411-
def pytest_collection_modifyitems(items, config):
412-
# Automatically mark tests based on fixtures they require.
413-
# Check pytest.ini or pytest --markers for marker descriptions.
414-
415-
markable_fixtures = [
416-
'uefi_vm',
417-
'unix_vm',
418-
'windows_vm',
419-
'hostA2',
420-
'hostB1',
421-
'sr_disk',
422-
'sr_disk_4k'
423-
]
424-
425-
for item in items:
426-
fixturenames = getattr(item, 'fixturenames', ())
427-
for fixturename in markable_fixtures:
428-
if fixturename in fixturenames:
429-
item.add_marker(fixturename)
430-
431-
if 'vm_ref' not in fixturenames:
432-
item.add_marker('no_vm')
433-
434-
if item.get_closest_marker('multi_vms'):
435-
# multi_vms implies small_vm
436-
item.add_marker('small_vm')

0 commit comments

Comments
 (0)