2 from atf_python.atf_pytest import ATFHandler
3 from typing import Dict
10 def set_handler(config):
11 global DEFAULT_HANDLER, PLUGIN_ENABLED
12 DEFAULT_HANDLER = ATFHandler(report_file_name=config.option.atf_file)
14 return DEFAULT_HANDLER
18 return DEFAULT_HANDLER
21 def pytest_addoption(parser):
24 group = parser.getgroup("general", "Running and selection options")
28 dest="atf_source_dir",
29 help="Path to the test source directory",
36 help="Call cleanup procedure for a given test",
38 group = parser.getgroup("terminal reporting", "reporting", after="general")
43 help="Enable test listing/results output in atf format",
49 help="Path to the status file provided by atf runtime",
53 @pytest.fixture(autouse=True, scope="session")
54 def atf_vars() -> Dict[str, str]:
55 return ATFHandler.get_atf_vars()
58 @pytest.hookimpl(trylast=True)
59 def pytest_configure(config):
60 if config.option.help:
63 # Register markings anyway to avoid warnings
64 config.addinivalue_line("markers", "require_user(name): user to run the test with")
65 config.addinivalue_line(
66 "markers", "require_arch(names): List[str] of support archs"
68 # config.addinivalue_line("markers", "require_config(config): List[Tuple[str,Any]] of k=v pairs")
69 config.addinivalue_line(
70 "markers", "require_diskspace(amount): str with required diskspace"
72 config.addinivalue_line(
73 "markers", "require_files(space): List[str] with file paths"
75 config.addinivalue_line(
76 "markers", "require_machine(names): List[str] of support machine types"
78 config.addinivalue_line(
79 "markers", "require_memory(amount): str with required memory"
81 config.addinivalue_line(
82 "markers", "require_progs(space): List[str] with file paths"
84 config.addinivalue_line(
85 "markers", "timeout(dur): int/float with max duration in sec"
88 if not config.option.atf:
90 handler = set_handler(config)
92 if config.option.collectonly:
93 # Need to output list of tests to stdout, hence override
94 # standard reporter plugin
95 reporter = config.pluginmanager.getplugin("terminalreporter")
97 config.pluginmanager.unregister(reporter)
99 handler.setup_configure()
102 def pytest_pycollect_makeitem(collector, name, obj):
104 handler = get_handler()
105 return handler.expand_tests(collector, name, obj)
108 def pytest_collection_modifyitems(session, config, items):
109 """If cleanup is requested, replace collected tests with their cleanups (if any)"""
111 handler = get_handler()
112 handler.modify_tests(items, config)
115 def pytest_collection_finish(session):
116 if PLUGIN_ENABLED and session.config.option.collectonly:
117 handler = get_handler()
118 handler.list_tests(session.items)
121 def pytest_runtest_setup(item):
123 handler = get_handler()
124 handler.setup_method_pre(item)
127 def pytest_runtest_logreport(report):
129 handler = get_handler()
130 handler.add_report(report)
133 def pytest_unconfigure(config):
134 if PLUGIN_ENABLED and config.option.atf_file:
135 handler = get_handler()
136 handler.write_report()