-
Notifications
You must be signed in to change notification settings - Fork 1
/
conftest.py
59 lines (49 loc) · 1.92 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
from os import environ
from datetime import datetime
from inspect import cleandoc
import json
import re
def pytest_configure(config):
config.pluginmanager.register(JSONPlugin(config))
class JSONPlugin:
def __init__(self, config):
self._report = {}
def pytest_runtest_makereport(self, item, call):
self._class_docstring = cleandoc(item.parent.obj.__doc__)
self._test_docstring = cleandoc(item.obj.__doc__)
def pytest_runtest_logreport(self, report):
if report.when != 'call':
return
json_output = {
'desc': self._test_docstring,
'args': re.findall(r'\[([^]]*)\]', report.head_line),
'outcome': report.outcome,
'error_msg': report.longreprtext,
}
module_name = re.match(r'tests/test_([^.]+)\.py',
report.fspath).group(1)
if module_name not in self._report:
class_desc = self._class_docstring[5:]
class_desc = class_desc[0].upper() + class_desc[1:]
self._report[module_name] = {
'desc': class_desc,
'results': {},
}
self._report[module_name]['results'][report.head_line] = json_output
def pytest_sessionfinish(self, session):
overall_status = 'healthy'
for v in self._report.values():
total = len(v['results'])
passed = len([x for x in v['results'].values()
if x['outcome'] == 'passed'])
v['status'] = 'healthy' if total == passed else 'unhealthy'
if v['status'] == 'unhealthy':
overall_status = 'unhealthy'
report = {
'created_at': str(datetime.utcnow()),
'build': environ.get('TRAVIS_BUILD_NUMBER'),
'status': overall_status,
'report': self._report,
}
with open('report.json', 'w') as f:
json.dump(report, f)