-
Notifications
You must be signed in to change notification settings - Fork 23
/
conftest.py
221 lines (186 loc) · 6.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
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
import csv
import os
import sys
import yaml
from pathlib import Path
import pytest
from geocoder_tester.base import assert_search, assert_reverse, CONFIG, API_TYPES
def pytest_collect_file(parent, path):
if not path.basename.startswith("test"):
return None
f = None
ext = path.ext
if ext == ".csv":
f = CSVFile.from_parent(parent, path=Path(path))
if ext == ".yml":
f = YamlFile.from_parent(parent, path=Path(path))
return f
def pytest_itemcollected(item):
dirs = item.session.fspath.bestrelpath(item.fspath.dirpath()).split(os.sep)
for d in dirs:
if d not in (".", "geocoder_tester", "world"):
item.add_marker(d)
if item.nodeid in CONFIG.get('COMPARE_WITH', []):
item.add_marker(
pytest.mark.xfail(run=not CONFIG['SKIP_XFAIL']))
def pytest_addoption(parser):
parser.addoption(
'--api-url',
dest="api_url",
default=CONFIG['API_URL'],
help="The URL to use for running tests against."
)
parser.addoption(
'--api-type',
dest="api_type",
default=CONFIG['API_TYPE'],
choices=API_TYPES.keys(),
help="The API to test against."
)
parser.addoption(
'--max-run',
dest="max_run",
type=int,
default=CONFIG['MAX_RUN'],
help="Limit the number of tests to be run."
)
parser.addoption(
'--loose-compare',
dest="loose_compare",
action="store_true",
help="Loose compare the results strings."
)
parser.addoption(
'--geojson', action="store_true", dest="geojson",
help=("Display geojson in traceback of failing tests.")
)
parser.addoption(
'--save-report',
dest="save_report",
help="Path where to save the report."
)
parser.addoption(
'--compare-report',
dest="compare_report",
help="Path where to load the report to compare with."
)
parser.addoption(
'--skip-xfail', action="store_true", dest="skip_xfail",
help="Do not run the tests known to fail when in compare mode."
)
def pytest_configure(config):
CONFIG['API_URL'] = config.getoption('--api-url')
CONFIG['API_TYPE'] = config.getoption('--api-type')
CONFIG['MAX_RUN'] = config.getoption('--max-run')
CONFIG['LOOSE_COMPARE'] = config.getoption('--loose-compare')
CONFIG['GEOJSON'] = config.getoption('--geojson')
CONFIG['SKIP_XFAIL'] = config.getoption('--skip-xfail')
if config.getoption('--compare-report'):
with open(config.getoption('--compare-report')) as f:
CONFIG['COMPARE_WITH'] = []
for line in f:
CONFIG['COMPARE_WITH'].append(line.rstrip('\r\n'))
def pytest_unconfigure(config):
if config.getoption('--save-report'):
with open(config.getoption('--save-report'), mode='w',
encoding='utf-8') as f:
f.write('\n'.join(CONFIG['FAILED']))
if config.getoption('--compare-report'):
import _pytest.config
writer = _pytest.config.create_terminal_writer(config, sys.stdout)
total = 0
writer.sep('!', 'NEW FAILURES', red=True)
for failed in CONFIG['FAILED']:
if failed not in CONFIG['COMPARE_WITH']:
total += 1
print(failed)
writer.sep('!', 'TOTAL NEW FAILURES: {}'.format(total), red=True)
total = 0
writer.sep('=', 'NEW PASSING', green=True)
for failed in CONFIG['COMPARE_WITH']:
if failed not in CONFIG['FAILED']:
print(failed)
total += 1
writer.sep('=', 'TOTAL NEW PASSING: {}'.format(total), green=True)
REPORTS = 0
def pytest_runtest_logreport(report):
if report.failed or (not report.passed and 'xfail' in report.keywords):
CONFIG['FAILED'].append(report.nodeid)
if report.when == 'teardown' and not report.skipped:
global REPORTS
REPORTS += 1
if CONFIG['MAX_RUN'] and REPORTS >= CONFIG['MAX_RUN']:
raise KeyboardInterrupt(
'Limit of {} reached'.format(CONFIG['MAX_RUN']))
class CSVFile(pytest.File):
def collect(self):
with self.path.open(encoding="utf-8") as f:
dialect = csv.Sniffer().sniff(f.read(2000))
f.seek(0)
reader = csv.DictReader(f, dialect=dialect)
for row in reader:
yield CSVItem.from_parent(self, row=row)
class YamlFile(pytest.File):
def collect(self):
raw = yaml.safe_load(self.path.open(encoding="utf-8"))
for name, spec in raw.items():
yield YamlItem.from_parent(self, name=name, spec=spec)
class BaseFlatItem(pytest.Item):
def __init__(self, name, parent, **kwargs):
super().__init__(name, parent)
self.lat = kwargs.get('lat')
self.lon = kwargs.get('lon')
self.lang = kwargs.get('lang')
self.limit = kwargs.get('limit')
self.comment = kwargs.get('comment')
self.skip = kwargs.get('skip')
self.detail = kwargs.get('detail')
self.mark = kwargs.get('mark', [])
for mark in self.mark:
self.add_marker(mark)
def runtest(self):
if self.skip is not None:
pytest.skip(msg=self.skip)
kwargs = {
'query': self.query,
'expected': self.expected,
'lang': self.lang,
'comment': self.comment
}
if self.lat and self.lon:
kwargs['center'] = [self.lat, self.lon]
if self.limit:
kwargs['limit'] = self.limit
if self.detail:
kwargs['detail'] = self.detail
if self.query:
assert_search(**kwargs)
elif 'center' in kwargs:
assert_reverse(**kwargs)
else:
pytest.skip(msg="Need at least parameters 'query' or 'lat/lon'.")
def repr_failure(self, excinfo):
""" called when self.runtest() raises an exception. """
return str(excinfo.value)
def reportstring(self):
s = "Search: {}".format(self.query)
if self.comment:
s = "{} ({})".format(s, self.comment)
return s
def reportinfo(self):
return self.fspath, 0, self.reportstring()
class CSVItem(BaseFlatItem):
def __init__(self, row, parent):
if "mark" in row:
row['mark'] = row['mark'].split(',')
super().__init__(row.get('query', ''), parent, **row)
self.query = row.get('query', '')
self.expected = {}
for key, value in row.items():
if key.startswith('expected_') and value:
self.expected[key[9:]] = value
class YamlItem(BaseFlatItem):
def __init__(self, name, parent, spec):
super(YamlItem, self).__init__(name, parent, **spec)
self.query = spec.pop('query', name)
self.expected = spec['expected']