aboutsummaryrefslogtreecommitdiff
path: root/tests/conftest.py
blob: 8e3c004b74d69190082008bceaa333631cc3c796 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import pytest
from atf_python.atf_pytest import ATFHandler
from typing import Dict


PLUGIN_ENABLED = False
DEFAULT_HANDLER = None


def set_handler(config):
    global DEFAULT_HANDLER, PLUGIN_ENABLED
    DEFAULT_HANDLER = ATFHandler(report_file_name=config.option.atf_file)
    PLUGIN_ENABLED = True
    return DEFAULT_HANDLER


def get_handler():
    return DEFAULT_HANDLER


def pytest_addoption(parser):
    """Add file output"""
    # Add meta-values
    group = parser.getgroup("general", "Running and selection options")
    group.addoption(
        "--atf-source-dir",
        type=str,
        dest="atf_source_dir",
        help="Path to the test source directory",
    )
    group.addoption(
        "--atf-cleanup",
        default=False,
        action="store_true",
        dest="atf_cleanup",
        help="Call cleanup procedure for a given test",
    )
    group = parser.getgroup("terminal reporting", "reporting", after="general")
    group.addoption(
        "--atf",
        default=False,
        action="store_true",
        help="Enable test listing/results output in atf format",
    )
    group.addoption(
        "--atf-file",
        type=str,
        dest="atf_file",
        help="Path to the status file provided by atf runtime",
    )


@pytest.fixture(autouse=True, scope="session")
def atf_vars() -> Dict[str, str]:
    return ATFHandler.get_atf_vars()


@pytest.hookimpl(trylast=True)
def pytest_configure(config):
    if config.option.help:
        return

    # Register markings anyway to avoid warnings
    config.addinivalue_line("markers", "require_user(name): user to run the test with")
    config.addinivalue_line(
        "markers", "require_arch(names): List[str] of support archs"
    )
    # config.addinivalue_line("markers", "require_config(config): List[Tuple[str,Any]] of k=v pairs")
    config.addinivalue_line(
        "markers", "require_diskspace(amount): str with required diskspace"
    )
    config.addinivalue_line(
        "markers", "require_files(space): List[str] with file paths"
    )
    config.addinivalue_line(
        "markers", "require_machine(names): List[str] of support machine types"
    )
    config.addinivalue_line(
        "markers", "require_memory(amount): str with required memory"
    )
    config.addinivalue_line(
        "markers", "require_progs(space): List[str] with file paths"
    )
    config.addinivalue_line(
        "markers", "timeout(dur): int/float with max duration in sec"
    )

    if not config.option.atf:
        return
    handler = set_handler(config)

    if config.option.collectonly:
        # Need to output list of tests to stdout, hence override
        # standard reporter plugin
        reporter = config.pluginmanager.getplugin("terminalreporter")
        if reporter:
            config.pluginmanager.unregister(reporter)
    else:
        handler.setup_configure()


def pytest_pycollect_makeitem(collector, name, obj):
    if PLUGIN_ENABLED:
        handler = get_handler()
        return handler.expand_tests(collector, name, obj)


def pytest_collection_modifyitems(session, config, items):
    """If cleanup is requested, replace collected tests with their cleanups (if any)"""
    if PLUGIN_ENABLED:
        handler = get_handler()
        handler.modify_tests(items, config)


def pytest_collection_finish(session):
    if PLUGIN_ENABLED and session.config.option.collectonly:
        handler = get_handler()
        handler.list_tests(session.items)


def pytest_runtest_setup(item):
    if PLUGIN_ENABLED:
        handler = get_handler()
        handler.setup_method_pre(item)


def pytest_runtest_logreport(report):
    if PLUGIN_ENABLED:
        handler = get_handler()
        handler.add_report(report)


def pytest_unconfigure(config):
    if PLUGIN_ENABLED and config.option.atf_file:
        handler = get_handler()
        handler.write_report()