| 1 |
import dataclasses |
| 2 |
import json |
| 3 |
from _colorize import get_colors # type: ignore[import-not-found] |
| 4 |
from typing import Any |
| 5 |
|
| 6 |
from .utils import ( |
| 7 |
StrJSON, TestName, FilterTuple, |
| 8 |
format_duration, normalize_test_name, print_warning) |
| 9 |
|
| 10 |
|
| 11 |
@dataclasses.dataclass(slots=True) |
| 12 |
class TestStats: |
| 13 |
tests_run: int = 0 |
| 14 |
failures: int = 0 |
| 15 |
skipped: int = 0 |
| 16 |
|
| 17 |
@staticmethod |
| 18 |
def from_unittest(result): |
| 19 |
return TestStats(result.testsRun, |
| 20 |
len(result.failures), |
| 21 |
len(result.skipped)) |
| 22 |
|
| 23 |
@staticmethod |
| 24 |
def from_doctest(results): |
| 25 |
return TestStats(results.attempted, |
| 26 |
results.failed, |
| 27 |
results.skipped) |
| 28 |
|
| 29 |
def accumulate(self, stats): |
| 30 |
self.tests_run += stats.tests_run |
| 31 |
self.failures += stats.failures |
| 32 |
self.skipped += stats.skipped |
| 33 |
|
| 34 |
|
| 35 |
# Avoid enum.Enum to reduce the number of imports when tests are run |
| 36 |
class State: |
| 37 |
PASSED = "PASSED" |
| 38 |
FAILED = "FAILED" |
| 39 |
SKIPPED = "SKIPPED" |
| 40 |
UNCAUGHT_EXC = "UNCAUGHT_EXC" |
| 41 |
REFLEAK = "REFLEAK" |
| 42 |
ENV_CHANGED = "ENV_CHANGED" |
| 43 |
RESOURCE_DENIED = "RESOURCE_DENIED" |
| 44 |
INTERRUPTED = "INTERRUPTED" |
| 45 |
WORKER_FAILED = "WORKER_FAILED" # non-zero worker process exit code |
| 46 |
WORKER_BUG = "WORKER_BUG" # exception when running a worker |
| 47 |
DID_NOT_RUN = "DID_NOT_RUN" |
| 48 |
TIMEOUT = "TIMEOUT" |
| 49 |
|
| 50 |
@staticmethod |
| 51 |
def is_failed(state): |
| 52 |
return state in { |
| 53 |
State.FAILED, |
| 54 |
State.UNCAUGHT_EXC, |
| 55 |
State.REFLEAK, |
| 56 |
State.WORKER_FAILED, |
| 57 |
State.WORKER_BUG, |
| 58 |
State.TIMEOUT} |
| 59 |
|
| 60 |
@staticmethod |
| 61 |
def has_meaningful_duration(state): |
| 62 |
# Consider that the duration is meaningless for these cases. |
| 63 |
# For example, if a whole test file is skipped, its duration |
| 64 |
# is unlikely to be the duration of executing its tests, |
| 65 |
# but just the duration to execute code which skips the test. |
| 66 |
return state not in { |
| 67 |
State.SKIPPED, |
| 68 |
State.RESOURCE_DENIED, |
| 69 |
State.INTERRUPTED, |
| 70 |
State.WORKER_FAILED, |
| 71 |
State.WORKER_BUG, |
| 72 |
State.DID_NOT_RUN} |
| 73 |
|
| 74 |
@staticmethod |
| 75 |
def must_stop(state): |
| 76 |
return state in { |
| 77 |
State.INTERRUPTED, |
| 78 |
State.WORKER_BUG, |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
FileName = str |
| 83 |
LineNo = int |
| 84 |
Location = tuple[FileName, LineNo] |
| 85 |
|
| 86 |
|
| 87 |
@dataclasses.dataclass(slots=True) |
| 88 |
class TestResult: |
| 89 |
test_name: TestName |
| 90 |
state: str | None = None |
| 91 |
# Test duration in seconds |
| 92 |
duration: float | None = None |
| 93 |
xml_data: list[str] | None = None |
| 94 |
stats: TestStats | None = None |
| 95 |
|
| 96 |
# errors and failures copied from support.TestFailedWithDetails |
| 97 |
errors: list[tuple[str, str]] | None = None |
| 98 |
failures: list[tuple[str, str]] | None = None |
| 99 |
|
| 100 |
# partial coverage in a worker run; not used by sequential in-process runs |
| 101 |
covered_lines: list[Location] | None = None |
| 102 |
|
| 103 |
def is_failed(self, fail_env_changed: bool) -> bool: |
| 104 |
if self.state == State.ENV_CHANGED: |
| 105 |
return fail_env_changed |
| 106 |
return State.is_failed(self.state) |
| 107 |
|
| 108 |
def _format_failed(self): |
| 109 |
ansi = get_colors() |
| 110 |
red, reset = ansi.BOLD_RED, ansi.RESET |
| 111 |
if self.errors and self.failures: |
| 112 |
le = len(self.errors) |
| 113 |
lf = len(self.failures) |
| 114 |
error_s = "error" + ("s" if le > 1 else "") |
| 115 |
failure_s = "failure" + ("s" if lf > 1 else "") |
| 116 |
return ( |
| 117 |
f"{red}{self.test_name} failed " |
| 118 |
f"({le} {error_s}, {lf} {failure_s}){reset}" |
| 119 |
) |
| 120 |
|
| 121 |
if self.errors: |
| 122 |
le = len(self.errors) |
| 123 |
error_s = "error" + ("s" if le > 1 else "") |
| 124 |
return f"{red}{self.test_name} failed ({le} {error_s}){reset}" |
| 125 |
|
| 126 |
if self.failures: |
| 127 |
lf = len(self.failures) |
| 128 |
failure_s = "failure" + ("s" if lf > 1 else "") |
| 129 |
return f"{red}{self.test_name} failed ({lf} {failure_s}){reset}" |
| 130 |
|
| 131 |
return f"{red}{self.test_name} failed{reset}" |
| 132 |
|
| 133 |
def __str__(self) -> str: |
| 134 |
ansi = get_colors() |
| 135 |
green = ansi.GREEN |
| 136 |
red = ansi.BOLD_RED |
| 137 |
reset = ansi.RESET |
| 138 |
yellow = ansi.YELLOW |
| 139 |
|
| 140 |
match self.state: |
| 141 |
case State.PASSED: |
| 142 |
return f"{green}{self.test_name} passed{reset}" |
| 143 |
case State.FAILED: |
| 144 |
return f"{red}{self._format_failed()}{reset}" |
| 145 |
case State.SKIPPED: |
| 146 |
return f"{yellow}{self.test_name} skipped{reset}" |
| 147 |
case State.UNCAUGHT_EXC: |
| 148 |
return ( |
| 149 |
f"{red}{self.test_name} failed (uncaught exception){reset}" |
| 150 |
) |
| 151 |
case State.REFLEAK: |
| 152 |
return f"{red}{self.test_name} failed (reference leak){reset}" |
| 153 |
case State.ENV_CHANGED: |
| 154 |
return f"{red}{self.test_name} failed (env changed){reset}" |
| 155 |
case State.RESOURCE_DENIED: |
| 156 |
return f"{yellow}{self.test_name} skipped (resource denied){reset}" |
| 157 |
case State.INTERRUPTED: |
| 158 |
return f"{yellow}{self.test_name} interrupted{reset}" |
| 159 |
case State.WORKER_FAILED: |
| 160 |
return ( |
| 161 |
f"{red}{self.test_name} worker non-zero exit code{reset}" |
| 162 |
) |
| 163 |
case State.WORKER_BUG: |
| 164 |
return f"{red}{self.test_name} worker bug{reset}" |
| 165 |
case State.DID_NOT_RUN: |
| 166 |
return f"{yellow}{self.test_name} ran no tests{reset}" |
| 167 |
case State.TIMEOUT: |
| 168 |
assert self.duration is not None, "self.duration is None" |
| 169 |
return f"{self.test_name} timed out ({format_duration(self.duration)})" |
| 170 |
case _: |
| 171 |
raise ValueError( |
| 172 |
f"{red}unknown result state: {{state!r}}{reset}" |
| 173 |
) |
| 174 |
|
| 175 |
def has_meaningful_duration(self): |
| 176 |
return State.has_meaningful_duration(self.state) |
| 177 |
|
| 178 |
def set_env_changed(self): |
| 179 |
if self.state is None or self.state == State.PASSED: |
| 180 |
self.state = State.ENV_CHANGED |
| 181 |
|
| 182 |
def must_stop(self, fail_fast: bool, fail_env_changed: bool) -> bool: |
| 183 |
if State.must_stop(self.state): |
| 184 |
return True |
| 185 |
if fail_fast and self.is_failed(fail_env_changed): |
| 186 |
return True |
| 187 |
return False |
| 188 |
|
| 189 |
def get_rerun_match_tests(self) -> FilterTuple | None: |
| 190 |
match_tests = [] |
| 191 |
|
| 192 |
errors = self.errors or [] |
| 193 |
failures = self.failures or [] |
| 194 |
for error_list, is_error in ( |
| 195 |
(errors, True), |
| 196 |
(failures, False), |
| 197 |
): |
| 198 |
for full_name, *_ in error_list: |
| 199 |
match_name = normalize_test_name(full_name, is_error=is_error) |
| 200 |
if match_name is None: |
| 201 |
# 'setUpModule (test.test_sys)': don't filter tests |
| 202 |
return None |
| 203 |
if not match_name: |
| 204 |
error_type = "ERROR" if is_error else "FAIL" |
| 205 |
print_warning(f"rerun failed to parse {error_type} test name: " |
| 206 |
f"{full_name!r}: don't filter tests") |
| 207 |
return None |
| 208 |
match_tests.append(match_name) |
| 209 |
|
| 210 |
if not match_tests: |
| 211 |
return None |
| 212 |
return tuple(match_tests) |
| 213 |
|
| 214 |
def write_json_into(self, file) -> None: |
| 215 |
json.dump(self, file, cls=_EncodeTestResult) |
| 216 |
|
| 217 |
@staticmethod |
| 218 |
def from_json(worker_json: StrJSON) -> 'TestResult': |
| 219 |
return json.loads(worker_json, object_hook=_decode_test_result) |
| 220 |
|
| 221 |
|
| 222 |
class _EncodeTestResult(json.JSONEncoder): |
| 223 |
def default(self, o: Any) -> dict[str, Any]: |
| 224 |
if isinstance(o, TestResult): |
| 225 |
result = dataclasses.asdict(o) |
| 226 |
result["__test_result__"] = o.__class__.__name__ |
| 227 |
return result |
| 228 |
else: |
| 229 |
return super().default(o) |
| 230 |
|
| 231 |
|
| 232 |
def _decode_test_result(data: dict[str, Any]) -> TestResult | dict[str, Any]: |
| 233 |
if "__test_result__" in data: |
| 234 |
data.pop('__test_result__') |
| 235 |
if data['stats'] is not None: |
| 236 |
data['stats'] = TestStats(**data['stats']) |
| 237 |
if data['covered_lines'] is not None: |
| 238 |
data['covered_lines'] = [ |
| 239 |
tuple(loc) for loc in data['covered_lines'] |
| 240 |
] |
| 241 |
return TestResult(**data) |
| 242 |
else: |
| 243 |
return data |