-
Notifications
You must be signed in to change notification settings - Fork 303
/
test_conftest.py
310 lines (228 loc) · 7.78 KB
/
test_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import inspect
import os
import platform
from pathlib import Path
from shutil import which
import pytest
from _pytest.config import ExitCode
from autotest.conftest import (
excludes_platform,
get_example_data_path,
get_project_root_path,
requires_exe,
requires_pkg,
requires_platform,
)
# temporary directory fixtures
def test_tmpdirs(tmpdir, module_tmpdir):
# function-scoped temporary directory
assert isinstance(tmpdir, Path)
assert tmpdir.is_dir()
assert inspect.currentframe().f_code.co_name in tmpdir.stem
# module-scoped temp dir (accessible to other tests in the script)
assert module_tmpdir.is_dir()
assert "autotest" in module_tmpdir.stem
def test_function_scoped_tmpdir(tmpdir):
assert isinstance(tmpdir, Path)
assert tmpdir.is_dir()
assert inspect.currentframe().f_code.co_name in tmpdir.stem
@pytest.mark.parametrize("name", ["noslash", "forward/slash", "back\\slash"])
def test_function_scoped_tmpdir_slash_in_name(tmpdir, name):
assert isinstance(tmpdir, Path)
assert tmpdir.is_dir()
# node name might have slashes if test function is parametrized
# (e.g., test_function_scoped_tmpdir_slash_in_name[a/slash])
replaced1 = name.replace("/", "_").replace("\\", "_").replace(":", "_")
replaced2 = name.replace("/", "_").replace("\\", "__").replace(":", "_")
assert (
f"{inspect.currentframe().f_code.co_name}[{replaced1}]" in tmpdir.stem
or f"{inspect.currentframe().f_code.co_name}[{replaced2}]"
in tmpdir.stem
)
class TestClassScopedTmpdir:
filename = "hello.txt"
@pytest.fixture(autouse=True)
def setup(self, class_tmpdir):
with open(class_tmpdir / self.filename, "w") as file:
file.write("hello, class-scoped tmpdir")
def test_class_scoped_tmpdir(self, class_tmpdir):
assert isinstance(class_tmpdir, Path)
assert class_tmpdir.is_dir()
assert self.__class__.__name__ in class_tmpdir.stem
assert Path(class_tmpdir / self.filename).is_file()
def test_module_scoped_tmpdir(module_tmpdir):
assert isinstance(module_tmpdir, Path)
assert module_tmpdir.is_dir()
assert Path(inspect.getmodulename(__file__)).stem in module_tmpdir.name
def test_session_scoped_tmpdir(session_tmpdir):
assert isinstance(session_tmpdir, Path)
assert session_tmpdir.is_dir()
# misc utilities
def test_get_project_root_path():
root = get_project_root_path()
assert root.is_dir()
contents = [p.name for p in root.glob("*")]
assert (
"autotest" in contents
and "README.md" in contents
)
def test_get_paths():
example_data = get_example_data_path()
project_root = get_project_root_path()
assert example_data.parent.parent == project_root
def test_get_example_data_path():
parts = get_example_data_path().parts
assert (
parts[-3] == "flopy"
and parts[-2] == "examples"
and parts[-1] == "data"
)
# requiring/excluding executables & platforms
@requires_exe("mf6")
def test_mf6():
assert which("mf6")
exes = ["mfusg", "mfnwt"]
@requires_exe(*exes)
def test_mfusg_and_mfnwt():
assert all(which(exe) for exe in exes)
@requires_pkg("numpy")
def test_numpy():
import numpy
assert numpy is not None
@requires_pkg("numpy", "matplotlib")
def test_numpy_and_matplotlib():
import matplotlib
import numpy
assert numpy is not None and matplotlib is not None
@requires_platform("Windows")
def test_needs_windows():
assert platform.system() == "Windows"
@excludes_platform("Darwin", ci_only=True)
def test_breaks_osx_ci():
if "CI" in os.environ:
assert platform.system() != "Darwin"
# meta-test marker and CLI argument --meta (-M)
@pytest.mark.meta("test_meta")
def test_meta_inner():
pass
class TestMeta:
def pytest_terminal_summary(self, terminalreporter):
stats = terminalreporter.stats
assert "failed" not in stats
passed = [test.head_line for test in stats["passed"]]
assert len(passed) == 1
assert test_meta_inner.__name__ in passed
deselected = [fn.name for fn in stats["deselected"]]
assert len(deselected) > 0
def test_meta():
args = [
f"{__file__}",
"-v",
"-s",
"-k",
test_meta_inner.__name__,
"-M",
"test_meta",
]
assert pytest.main(args, plugins=[TestMeta()]) == ExitCode.OK
# CLI arguments --keep (-K) and --keep-failed
FILE_NAME = "hello.txt"
@pytest.mark.meta("test_keep")
def test_keep_function_scoped_tmpdir_inner(tmpdir):
with open(tmpdir / FILE_NAME, "w") as f:
f.write("hello, function-scoped tmpdir")
@pytest.mark.meta("test_keep")
class TestKeepClassScopedTmpdirInner:
def test_keep_class_scoped_tmpdir_inner(self, class_tmpdir):
with open(class_tmpdir / FILE_NAME, "w") as f:
f.write("hello, class-scoped tmpdir")
@pytest.mark.meta("test_keep")
def test_keep_module_scoped_tmpdir_inner(module_tmpdir):
with open(module_tmpdir / FILE_NAME, "w") as f:
f.write("hello, module-scoped tmpdir")
@pytest.mark.meta("test_keep")
def test_keep_session_scoped_tmpdir_inner(session_tmpdir):
with open(session_tmpdir / FILE_NAME, "w") as f:
f.write("hello, session-scoped tmpdir")
@pytest.mark.parametrize("arg", ["--keep", "-K"])
def test_keep_function_scoped_tmpdir(tmpdir, arg):
inner_fn = test_keep_function_scoped_tmpdir_inner.__name__
args = [
__file__,
"-v",
"-s",
"-k",
inner_fn,
"-M",
"test_keep",
"-K",
tmpdir,
]
assert pytest.main(args) == ExitCode.OK
assert Path(tmpdir / f"{inner_fn}0" / FILE_NAME).is_file()
@pytest.mark.parametrize("arg", ["--keep", "-K"])
def test_keep_class_scoped_tmpdir(tmpdir, arg):
args = [
__file__,
"-v",
"-s",
"-k",
TestKeepClassScopedTmpdirInner.test_keep_class_scoped_tmpdir_inner.__name__,
"-M",
"test_keep",
"-K",
tmpdir,
]
assert pytest.main(args) == ExitCode.OK
assert Path(
tmpdir / f"{TestKeepClassScopedTmpdirInner.__name__}0" / FILE_NAME
).is_file()
@pytest.mark.parametrize("arg", ["--keep", "-K"])
def test_keep_module_scoped_tmpdir(tmpdir, arg):
args = [
__file__,
"-v",
"-s",
"-k",
test_keep_module_scoped_tmpdir_inner.__name__,
"-M",
"test_keep",
"-K",
tmpdir,
]
assert pytest.main(args) == ExitCode.OK
this_file_path = Path(__file__)
this_test_dir = (
tmpdir
/ f"{str(this_file_path.parent.name)}.{str(this_file_path.stem)}0"
)
assert FILE_NAME in [f.name for f in this_test_dir.glob("*")]
@pytest.mark.parametrize("arg", ["--keep", "-K"])
def test_keep_session_scoped_tmpdir(tmpdir, arg, request):
args = [
__file__,
"-v",
"-s",
"-k",
test_keep_session_scoped_tmpdir_inner.__name__,
"-M",
"test_keep",
"-K",
tmpdir,
]
assert pytest.main(args) == ExitCode.OK
assert Path(tmpdir / f"{request.session.name}0" / FILE_NAME).is_file()
@pytest.mark.meta("test_keep_failed")
def test_keep_failed_function_scoped_tmpdir_inner(tmpdir):
with open(tmpdir / FILE_NAME, "w") as f:
f.write("hello, function-scoped tmpdir")
assert False, "oh no"
@pytest.mark.parametrize("keep", [True, False])
def test_keep_failed_function_scoped_tmpdir(tmpdir, keep):
inner_fn = test_keep_failed_function_scoped_tmpdir_inner.__name__
args = [__file__, "-v", "-s", "-k", inner_fn, "-M", "test_keep_failed"]
if keep:
args += ["--keep-failed", tmpdir]
assert pytest.main(args) == ExitCode.TESTS_FAILED
kept_file = Path(tmpdir / f"{inner_fn}0" / FILE_NAME).is_file()
assert kept_file if keep else not kept_file