2 from atf_python.atf_pytest import ATFHandler
3 from typing import Dict
11 global DEFAULT_HANDLER
12 if DEFAULT_HANDLER is None:
13 DEFAULT_HANDLER = ATFHandler()
14 return DEFAULT_HANDLER
17 def pytest_addoption(parser):
20 group = parser.getgroup("general", "Running and selection options")
24 dest="atf_source_dir",
25 help="Path to the test source directory",
32 help="Call cleanup procedure for a given test",
34 group = parser.getgroup("terminal reporting", "reporting", after="general")
39 help="Enable test listing/results output in atf format",
45 help="Path to the status file provided by atf runtime",
49 @pytest.fixture(autouse=True, scope="session")
50 def atf_vars() -> Dict[str, str]:
51 return ATFHandler.get_atf_vars()
54 @pytest.hookimpl(trylast=True)
55 def pytest_configure(config):
56 if config.option.help:
59 # Register markings anyway to avoid warnings
60 config.addinivalue_line("markers", "require_user(name): user to run the test with")
61 config.addinivalue_line(
62 "markers", "require_arch(names): List[str] of support archs"
64 # config.addinivalue_line("markers", "require_config(config): List[Tuple[str,Any]] of k=v pairs")
65 config.addinivalue_line(
66 "markers", "require_diskspace(amount): str with required diskspace"
68 config.addinivalue_line(
69 "markers", "require_files(space): List[str] with file paths"
71 config.addinivalue_line(
72 "markers", "require_machine(names): List[str] of support machine types"
74 config.addinivalue_line(
75 "markers", "require_memory(amount): str with required memory"
77 config.addinivalue_line(
78 "markers", "require_progs(space): List[str] with file paths"
80 config.addinivalue_line(
81 "markers", "timeout(dur): int/float with max duration in sec"
85 PLUGIN_ENABLED = config.option.atf
86 if not PLUGIN_ENABLED:
90 if config.option.collectonly:
91 # Need to output list of tests to stdout, hence override
92 # standard reporter plugin
93 reporter = config.pluginmanager.getplugin("terminalreporter")
95 config.pluginmanager.unregister(reporter)
98 def pytest_collection_modifyitems(session, config, items):
99 """If cleanup is requested, replace collected tests with their cleanups (if any)"""
100 if PLUGIN_ENABLED and config.option.atf_cleanup:
102 handler = get_handler()
104 if handler.has_object_cleanup(obj):
105 handler.override_runtest(obj)
106 new_items.append(obj)
108 items.extend(new_items)
111 def pytest_collection_finish(session):
112 if PLUGIN_ENABLED and session.config.option.collectonly:
113 handler = get_handler()
114 handler.list_tests(session.items)
117 def pytest_runtest_logreport(report):
119 handler = get_handler()
120 handler.add_report(report)
123 def pytest_unconfigure(config):
124 if PLUGIN_ENABLED and config.option.atf_file:
125 handler = get_handler()
126 handler.write_report(config.option.atf_file)