-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_runner.py
86 lines (61 loc) · 2.7 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
84
85
86
"""
Not currently used.
This is a custom test runner that outputs test results in JSON format.
To use it, uncomment this config/settings.py line:
`# TEST_RUNNER = 'test_runner.JSONTestRunner'`
"""
import json
import sys
from unittest.runner import TextTestResult
from django.test.runner import DiscoverRunner
class JSONTestResult(TextTestResult):
def __init__(self, stream, descriptions, verbosity):
super().__init__(stream, descriptions, verbosity)
self.results = []
def addSuccess(self, test):
super().addSuccess(test)
self.results.append({'test': str(test), 'status': 'success'})
def addFailure(self, test, err):
super().addFailure(test, err)
self.results.append({'test': str(test), 'status': 'failure', 'error': self._exc_info_to_string(err, test)})
def addError(self, test, err):
super().addError(test, err)
self.results.append({'test': str(test), 'status': 'error', 'error': self._exc_info_to_string(err, test)})
def addSkip(self, test, reason):
super().addSkip(test, reason)
self.results.append({'test': str(test), 'status': 'skipped', 'reason': reason})
def addExpectedFailure(self, test, err):
super().addExpectedFailure(test, err)
self.results.append({'test': str(test), 'status': 'expected failure', 'error': self._exc_info_to_string(err, test)})
def addUnexpectedSuccess(self, test):
super().addUnexpectedSuccess(test)
self.results.append({'test': str(test), 'status': 'unexpected success'})
class JSONTestRunner(DiscoverRunner):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Default descriptions and verbosity for compatibility
self.descriptions = kwargs.get('descriptions', True)
self.verbosity = kwargs.get('verbosity', 1)
def run_suite(self, suite, **kwargs):
# Wrap sys.stdout with StreamWrapper to ensure compatibility
stream = StreamWrapper(sys.stdout)
# Initialize the custom result class with descriptions and verbosity
result = JSONTestResult(stream, self.descriptions, self.verbosity)
# Run the test suite with the custom result
suite.run(result)
# Display results as JSON
self.display_results_as_json(result)
return result
@staticmethod
def display_results_as_json(result):
output = {'tests': result.results}
print(json.dumps(output, indent=4))
class StreamWrapper:
def __init__(self, stream):
self.stream = stream
def write(self, message):
self.stream.write(message)
def writeln(self, message):
self.stream.write(message + '\n')
def flush(self):
self.stream.flush()