-
Notifications
You must be signed in to change notification settings - Fork 87
/
conftest.py
151 lines (122 loc) · 3.82 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
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
import os
import shutil
import glob
from os.path import join, dirname
import pytest
# ------------------------- Paths constants
# these are added to pytest namespace for convenience if finding things
# eg test data
PKG_PATH = join(dirname(__file__), 'eqcorrscan')
TEST_PATH = join(PKG_PATH, 'tests')
TEST_DATA_PATH = join(TEST_PATH, 'test_data')
# --------------- pytest configuration
def pytest_addoption(parser):
try:
parser.addoption("--runslow", action="store_true",
help="run slow tests")
except ValueError as e:
print(e)
pass
try:
parser.addoption("--runsuperslow", action="store_true",
help="run super-slow tests")
except ValueError as e:
print(e)
pass
# ------------------ session fixtures
@pytest.fixture(scope='session', autouse=True)
def clean_up_test_files():
""" cleanup the file droppings produced by doc tests.
This fixture will run after all other tests have finished """
files_to_kill = [
'test_csv_write.csv',
'test_family.tgz',
'test_quakeml.xml',
'test_tar_write.tgz',
'test_template.tgz',
'test_template_read.tgz',
'test_tribe.tgz',
'test_waveforms.ms',
'mag_calc.out',
'station.dat',
'station_elev.dat',
'test_waveform.ms',
'01-0410-35L.S201309',
'04-0007-55R.S201601',
'04-0045-52L.S201601',
'dt.cc',
'dt.cc2',
'dt.ct',
'dt.ct2',
'phase.dat',
'eqcorrscan_temporary_party.pkl'
]
yield
# remove files
for fi in files_to_kill:
if os.path.isfile(fi):
try:
os.remove(fi)
except Exception as e:
print("File not found, assuming already cleaned")
print(e)
@pytest.fixture(scope='session', autouse=True)
def clean_up_test_directories():
""" cleanup the file droppings produced by doc tests.
This fixture will run after all other tests have finished """
directories_to_kill = [
'temp1',
'temp2',
'test_family',
'test_party_out',
'test_tar_write',
'tmp1',
'cc_exported',
'.streams',
'.parties'
]
directories_to_kill.extend(glob.glob(".template_db_*"))
directories_to_kill.extend(glob.glob(".streams_*"))
yield
# remove files
for directory in directories_to_kill:
if os.path.isdir(directory):
try:
print(f"Removing directory {directory}")
shutil.rmtree(directory)
except Exception as e:
print("Could not find directory, already cleaned?")
print(e)
# ------------- add objects to global pytest scope
def append_name(list_like):
"""
Decorator to append a function name to an object with an append method.
Useful for making meta-fixtures using the following recipe:
https://github.com/pytest-dev/pytest/issues/349#issuecomment-189370273
:param list_like: Any object with append method
:return: func
"""
def _append_func_name(func):
list_like.append(func.__name__)
return func
return _append_func_name
# Stop-gap as per
# https://docs.pytest.org/en/latest/deprecations.html#pytest-namespace
def pytest_configure():
pytest.append_name = append_name
pytest.test_path = TEST_PATH
pytest.test_data_path = TEST_DATA_PATH
pytest.pkg_path = PKG_PATH
# Over-ride the -n auto in travis as this goes of the wall
# import sys
# import os
#
#
# def pytest_cmdline_preparse(config, args):
# is_travis = 'TRAVIS' in os.environ
# if 'xdist' in sys.modules and not is_travis:
# if "-n" in args:
# args[args.index("-n") + 1] = "2"
# else:
# args[:] = args + ["-n2"]
# print(args)