-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_tests.py
111 lines (92 loc) Β· 4.74 KB
/
run_tests.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
import os
import sys
import unittest
class Result(unittest.TextTestResult):
def __init__(self, stream, descriptions, verbosity):
super().__init__(stream, descriptions, verbosity)
if os.getenv("GITHUB_STEP_SUMMARY"):
self.job_summary = open(os.getenv("GITHUB_STEP_SUMMARY"), "a", encoding="utf-8")
self.job_summary.write(f"# Test Results (Python {'.'.join(map(str, sys.version_info[:3]))})\n\n")
self.job_summary.write("π = `mcc_api.event`\n")
self.job_summary.write("ποΈ = `mcc_api.island`\n")
self.job_summary.write(" |Name|Result\n-|-|-\n")
else:
self.job_summary = None
def getTestName(self, test):
name_parts = test.id().split(".")
match name_parts[0]:
case "event": return f"π|{name_parts[-1]}"
case "island": return f"ποΈ|{name_parts[-1]}"
def addSuccess(self, test):
super().addSuccess(test)
if self.job_summary:
self.job_summary.write(f"{self.getTestName(test)}|β
\n")
def addError(self, test, err):
super().addError(test, err)
if self.job_summary:
self.job_summary.write(f"{self.getTestName(test)}|β\n")
def addFailure(self, test, err):
super().addFailure(test, err)
if self.job_summary:
self.job_summary.write(f"{self.getTestName(test)}|β\n")
def addSkip(self, test, reason):
super().addSkip(test, reason)
if self.job_summary:
self.job_summary.write(f"{self.getTestName(test)}|βͺοΈ\n")
def addExpectedFailure(self, test, err):
super().addExpectedFailure(test, err)
if self.job_summary:
self.job_summary.write(f"{self.getTestName(test)}|β
\n")
def addUnexpectedSuccess(self, test):
super().addUnexpectedSuccess(test)
if self.job_summary:
self.job_summary.write(f"{self.getTestName(test)}|β\n")
def printErrors(self):
super().printErrors()
if self.job_summary:
self.job_summary.write("# Statistics\n")
errors = len(self.errors)
expected_failures = len(self.expectedFailures)
failures = len(self.failures)
skipped = len(self.skipped)
unexpected_successes = len(self.unexpectedSuccesses)
successes = self.testsRun - (errors + expected_failures + failures + skipped + unexpected_successes)
self.job_summary.write(f"- Tests Run: {self.testsRun}\n")
self.job_summary.write(f"- Successes: {successes}\n")
self.job_summary.write(f"- Errors: {errors}\n")
self.job_summary.write(f"- Expected Failures: {expected_failures}\n")
self.job_summary.write(f"- Failures: {failures}\n")
self.job_summary.write(f"- Skipped: {skipped}\n")
self.job_summary.write(f"- Unexpected Successes: {unexpected_successes}\n")
if self.errors:
self.job_summary.write("# Errors\n")
for test, err in self.errors:
test_name = self.getDescription(test).split()[1].strip("()")
self.job_summary.write(f"## {test_name}\n")
self.job_summary.write(f"```python-traceback\n{err.rstrip()}\n```\n")
if self.failures:
self.job_summary.write("# Failures\n")
for test, err in self.failures:
test_name = self.getDescription(test).split()[1].strip("()")
self.job_summary.write(f"## {test_name}\n")
self.job_summary.write(f"```python-traceback\n{err.rstrip()}\n```\n")
if self.unexpectedSuccesses:
self.job_summary.write("# Unexpected Successes\n")
for test in self.unexpectedSuccesses:
test_name = self.getDescription(test).split()[1].strip("()")
self.job_summary.write(f"## {test_name}\n")
def __del__(self):
if "job_summary" in self.__dict__ and self.job_summary:
self.job_summary.close()
class Runner(unittest.TextTestRunner):
def __init__(self, stream=None, descriptions=True, verbosity=1,
failfast=False, buffer=False, resultclass=Result, warnings=None,
*, tb_locals=False):
super().__init__(stream, descriptions, verbosity, failfast, buffer, resultclass, warnings, tb_locals=tb_locals)
if __name__ == "__main__":
if job_summary_path := os.getenv("GITHUB_STEP_SUMMARY"):
with open(job_summary_path, "w", encoding="utf-8") as job_summary:
job_summary.write("")
tests: unittest.TestSuite = unittest.TestLoader().discover(".")
result: unittest.TestResult = Runner(verbosity=2).run(tests)
sys.exit(not result.wasSuccessful())