xref: /freebsd/tests/conftest.py (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1import pytest
2from atf_python.atf_pytest import ATFHandler
3
4
5PLUGIN_ENABLED = False
6DEFAULT_HANDLER = None
7
8
9def get_handler():
10    global DEFAULT_HANDLER
11    if DEFAULT_HANDLER is None:
12        DEFAULT_HANDLER = ATFHandler()
13    return DEFAULT_HANDLER
14
15
16def pytest_addoption(parser):
17    """Add file output"""
18    # Add meta-values
19    group = parser.getgroup("general", "Running and selection options")
20    group.addoption("--atf-var", dest="atf_vars", action="append", default=[])
21    group.addoption(
22        "--atf-source-dir",
23        type=str,
24        dest="atf_source_dir",
25        help="Path to the test source directory",
26    )
27    group.addoption(
28        "--atf-cleanup",
29        default=False,
30        action="store_true",
31        dest="atf_cleanup",
32        help="Call cleanup procedure for a given test",
33    )
34    group = parser.getgroup("terminal reporting", "reporting", after="general")
35    group.addoption(
36        "--atf",
37        default=False,
38        action="store_true",
39        help="Enable test listing/results output in atf format",
40    )
41    group.addoption(
42        "--atf-file",
43        type=str,
44        dest="atf_file",
45        help="Path to the status file provided by atf runtime",
46    )
47
48
49@pytest.mark.trylast
50def pytest_configure(config):
51    if config.option.help:
52        return
53
54    # Register markings anyway to avoid warnings
55    config.addinivalue_line("markers", "require_user(name): user to run the test with")
56    config.addinivalue_line(
57        "markers", "require_arch(names): List[str] of support archs"
58    )
59    # config.addinivalue_line("markers", "require_config(config): List[Tuple[str,Any]] of k=v pairs")
60    config.addinivalue_line(
61        "markers", "require_diskspace(amount): str with required diskspace"
62    )
63    config.addinivalue_line(
64        "markers", "require_files(space): List[str] with file paths"
65    )
66    config.addinivalue_line(
67        "markers", "require_machine(names): List[str] of support machine types"
68    )
69    config.addinivalue_line(
70        "markers", "require_memory(amount): str with required memory"
71    )
72    config.addinivalue_line(
73        "markers", "require_progs(space): List[str] with file paths"
74    )
75    config.addinivalue_line(
76        "markers", "timeout(dur): int/float with max duration in sec"
77    )
78
79    global PLUGIN_ENABLED
80    PLUGIN_ENABLED = config.option.atf
81    if not PLUGIN_ENABLED:
82        return
83    get_handler()
84
85    if config.option.collectonly:
86        # Need to output list of tests to stdout, hence override
87        # standard reporter plugin
88        reporter = config.pluginmanager.getplugin("terminalreporter")
89        if reporter:
90            config.pluginmanager.unregister(reporter)
91
92
93def pytest_collection_modifyitems(session, config, items):
94    """If cleanup is requested, replace collected tests with their cleanups (if any)"""
95    if PLUGIN_ENABLED and config.option.atf_cleanup:
96        new_items = []
97        handler = get_handler()
98        for obj in items:
99            if handler.has_object_cleanup(obj):
100                handler.override_runtest(obj)
101                new_items.append(obj)
102        items.clear()
103        items.extend(new_items)
104
105
106def pytest_collection_finish(session):
107    if PLUGIN_ENABLED and session.config.option.collectonly:
108        handler = get_handler()
109        handler.list_tests(session.items)
110
111
112def pytest_runtest_logreport(report):
113    if PLUGIN_ENABLED:
114        handler = get_handler()
115        handler.add_report(report)
116
117
118def pytest_unconfigure(config):
119    if PLUGIN_ENABLED and config.option.atf_file:
120        handler = get_handler()
121        handler.write_report(config.option.atf_file)
122