/
conftest.py
112 lines (93 loc) · 3.63 KB
/
conftest.py
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
"""PyTest local plugins."""
import os
import pytest
SKIPPING_CONFIG = {
"slow": {
"options": ["--run-only-slow", "--slow"],
"help": "run the slow tests",
"description": "mark test as slow to run",
"skip_otherwise": True,
},
"cypress": {
"options": ["--run-only-yarn", "--cypress"],
"help": "run the cypress tests",
"description": "mark test as using cypress",
"skip_otherwise": True,
},
"pydicom": {
"options": ["--run-only-pydicom", "--pydicom"],
"help": "run only the tests that use pydicom",
"description": "mark test as using pydicom",
"skip_otherwise": False,
},
"mosaiqdb": {
"options": ["--run-only-mosaiqdb", "--mosaiqdb"],
"help": "run only the tests that use mosaiq db",
"description": "mark test as using mosaiq db",
"skip_otherwise": True,
},
"all": {
"options": ["--run-all-tests", "--all"],
"help": "run all tests",
"description": "run all tests that would normally be skipped due to a configuration flag",
"skip_otherwise": False,
},
}
# https://docs.pytest.org/en/latest/example/simple.html#control-skipping-of-tests-according-to-command-line-option
def pytest_addoption(parser):
for _, skip_item in SKIPPING_CONFIG.items():
for option in skip_item["options"]:
parser.addoption(
option, action="store_true", default=False, help=skip_item["help"]
)
def pytest_configure(config):
for key, skip_item in SKIPPING_CONFIG.items():
config.addinivalue_line("markers", f"{key}: {skip_item['description']}")
def pytest_collection_modifyitems(config, items):
for option in SKIPPING_CONFIG["all"]["options"]:
if config.getoption(option):
return
for key, skip_item in SKIPPING_CONFIG.items():
this_option_set = False
provided_option = ""
for option in skip_item["options"]:
if config.getoption(option):
this_option_set = True
provided_option = option
break
if not this_option_set:
if skip_item["skip_otherwise"]:
skip = pytest.mark.skip(
reason=f"need {skip_item['options'][-1]} option to run"
)
for item in items:
if key in item.keywords:
item.add_marker(skip)
else:
skip = pytest.mark.skip(reason=f"since {provided_option} was passed")
for item in items:
if key not in item.keywords:
item.add_marker(skip)
def pytest_ignore_collect(path, config): # pylint: disable = unused-argument
"""return True to prevent considering this path for collection.
This hook is consulted for all files and directories prior to
calling more specific hooks.
"""
relative_path = os.path.relpath(str(path), os.path.dirname(__file__))
relative_path_list = relative_path.split(os.path.sep)
return (
(len(relative_path_list) > 1 and relative_path_list[0] == "examples")
or "node_modules" in relative_path_list
or "site-packages" in relative_path_list
or "_build" in relative_path_list
or ("_bundle" in relative_path_list and "python" in relative_path_list)
or (
config.getoption("--doctest-modules")
and (
"_gamma" in relative_path_list
or "tests" in relative_path_list
or "_imports" in relative_path_list
or "_experimental" in relative_path_list
)
)
)