-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcustom_test_runner.py
60 lines (40 loc) · 2.09 KB
/
custom_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
import itertools
import unittest.runner
class CustomTextTestResult(unittest.runner.TextTestResult):
"""Extension of TextTestResult to support numbering test cases"""
def __init__(self, stream, descriptions, verbosity):
"""Initializes the test number generator, then calls super impl"""
self.test_numbers = itertools.count(1)
return super(CustomTextTestResult, self).__init__(stream, descriptions, verbosity)
def startTest(self, test):
"""Writes the test number to the stream if showAll is set, then calls super impl"""
if self.showAll:
test_numbers = next(self.test_numbers)
test_case_count = self.test_case_count
progress = f"[{test_numbers}/{test_case_count}] "
self.stream.write(progress)
# Also store the progress in the test itself, so that if it errors,
# it can be written to the exception information by our overridden
# _exec_info_to_string method:
test.progress_index = progress
return super(CustomTextTestResult, self).startTest(test)
def _exc_info_to_string(self, err, test):
"""Gets an exception info string from super, and prepends 'Test Number' line"""
info = super(CustomTextTestResult, self)._exc_info_to_string(err, test)
if self.showAll:
index = test.progress_index
test_info = info
info = f"Test number: {index}\n{test_info}"
return info
class CustomTextTestRunner(unittest.runner.TextTestRunner):
"""Extension of TextTestRunner to support numbering test cases"""
resultclass = CustomTextTestResult
def run(self, test):
"""Stores the total count of test cases, then calls super impl"""
self.test_case_count = test.countTestCases()
return super(CustomTextTestRunner, self).run(test)
def _makeResult(self):
"""Creates and returns a result instance that knows the count of test cases"""
result = super(CustomTextTestRunner, self)._makeResult()
result.test_case_count = self.test_case_count
return result