-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_runner.py
83 lines (68 loc) · 2.29 KB
/
test_runner.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
import json
import unittest
import time
class JSONTestResult(unittest.TextTestResult):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.results = {
'passed': [],
'failed': []
}
def addSuccess(self, test):
super().addSuccess(test)
self.results['passed'].append(
{
'status': 'success',
'test_scenario_name': test._testMethodName
}
)
def addError(self, test, err):
super().addError(test, err)
failed_output = self._exc_info_to_string(err, test).split('\n')[-2]
self.results['failed'].append(
{
'status': 'error',
'test_scenario_name': test._testMethodName,
'message': failed_output
}
)
def addFailure(self, test, err):
super().addFailure(test, err)
failed_output = self._exc_info_to_string(err, test).split('\n')[-2]
self.results['failed'].append(
{
'status': 'failure',
'test_scenario_name': test._testMethodName,
'message': failed_output
}
)
def addSkip(self, test, err):
super().addSkip(test, err)
failed_output = self._exc_info_to_string(err, test).split('\n')[-2]
self.results['failed'].append(
{
'status': 'skip',
'test_scenario_name': test._testMethodName,
'message': failed_output
}
)
class JSONTestRunner(unittest.TextTestRunner):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def run_tests():
test_dir = 'tests'
suite = unittest.TestSuite()
loader = unittest.TestLoader()
suite.addTests(loader.discover(test_dir))
runner = JSONTestRunner(verbosity = 2, resultclass=JSONTestResult)
start_time = time.time()
result = runner.run(suite)
end_time = time.time()
structure_data = {
'results': result.results,
'time_taken': round(end_time - start_time, 3),
}
with open('json-test-runner.json', 'w') as structure_json_output_file:
json.dump(structure_data,structure_json_output_file,indent=2)
if __name__ == '__main__':
run_tests()