| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from _pytest.pytester import Pytester |
| 4 |
import pytest |
| 5 |
|
| 6 |
|
| 7 |
@pytest.mark.filterwarnings("default::pytest.PytestUnhandledThreadExceptionWarning") |
| 8 |
def test_unhandled_thread_exception(pytester: Pytester) -> None: |
| 9 |
pytester.makepyfile( |
| 10 |
test_it=""" |
| 11 |
import threading |
| 12 |
|
| 13 |
def test_it(): |
| 14 |
def oops(): |
| 15 |
raise ValueError("Oops") |
| 16 |
|
| 17 |
t = threading.Thread(target=oops, name="MyThread") |
| 18 |
t.start() |
| 19 |
t.join() |
| 20 |
|
| 21 |
def test_2(): pass |
| 22 |
""" |
| 23 |
) |
| 24 |
result = pytester.runpytest() |
| 25 |
assert result.ret == 0 |
| 26 |
result.assert_outcomes(passed=2, warnings=1) |
| 27 |
result.stdout.fnmatch_lines( |
| 28 |
[ |
| 29 |
"*= warnings summary =*", |
| 30 |
"test_it.py::test_it", |
| 31 |
" * PytestUnhandledThreadExceptionWarning: Exception in thread MyThread", |
| 32 |
" ", |
| 33 |
" Traceback (most recent call last):", |
| 34 |
" ValueError: Oops", |
| 35 |
" ", |
| 36 |
" warnings.warn(pytest.PytestUnhandledThreadExceptionWarning(msg))", |
| 37 |
] |
| 38 |
) |
| 39 |
|
| 40 |
|
| 41 |
@pytest.mark.filterwarnings("default::pytest.PytestUnhandledThreadExceptionWarning") |
| 42 |
def test_unhandled_thread_exception_in_setup(pytester: Pytester) -> None: |
| 43 |
pytester.makepyfile( |
| 44 |
test_it=""" |
| 45 |
import threading |
| 46 |
import pytest |
| 47 |
|
| 48 |
@pytest.fixture |
| 49 |
def threadexc(): |
| 50 |
def oops(): |
| 51 |
raise ValueError("Oops") |
| 52 |
t = threading.Thread(target=oops, name="MyThread") |
| 53 |
t.start() |
| 54 |
t.join() |
| 55 |
|
| 56 |
def test_it(threadexc): pass |
| 57 |
def test_2(): pass |
| 58 |
""" |
| 59 |
) |
| 60 |
result = pytester.runpytest() |
| 61 |
assert result.ret == 0 |
| 62 |
result.assert_outcomes(passed=2, warnings=1) |
| 63 |
result.stdout.fnmatch_lines( |
| 64 |
[ |
| 65 |
"*= warnings summary =*", |
| 66 |
"test_it.py::test_it", |
| 67 |
" * PytestUnhandledThreadExceptionWarning: Exception in thread MyThread", |
| 68 |
" ", |
| 69 |
" Traceback (most recent call last):", |
| 70 |
" ValueError: Oops", |
| 71 |
" ", |
| 72 |
" warnings.warn(pytest.PytestUnhandledThreadExceptionWarning(msg))", |
| 73 |
] |
| 74 |
) |
| 75 |
|
| 76 |
|
| 77 |
@pytest.mark.filterwarnings("default::pytest.PytestUnhandledThreadExceptionWarning") |
| 78 |
def test_unhandled_thread_exception_in_teardown(pytester: Pytester) -> None: |
| 79 |
pytester.makepyfile( |
| 80 |
test_it=""" |
| 81 |
import threading |
| 82 |
import pytest |
| 83 |
|
| 84 |
@pytest.fixture |
| 85 |
def threadexc(): |
| 86 |
def oops(): |
| 87 |
raise ValueError("Oops") |
| 88 |
yield |
| 89 |
t = threading.Thread(target=oops, name="MyThread") |
| 90 |
t.start() |
| 91 |
t.join() |
| 92 |
|
| 93 |
def test_it(threadexc): pass |
| 94 |
def test_2(): pass |
| 95 |
""" |
| 96 |
) |
| 97 |
result = pytester.runpytest() |
| 98 |
assert result.ret == 0 |
| 99 |
result.assert_outcomes(passed=2, warnings=1) |
| 100 |
result.stdout.fnmatch_lines( |
| 101 |
[ |
| 102 |
"*= warnings summary =*", |
| 103 |
"test_it.py::test_it", |
| 104 |
" * PytestUnhandledThreadExceptionWarning: Exception in thread MyThread", |
| 105 |
" ", |
| 106 |
" Traceback (most recent call last):", |
| 107 |
" ValueError: Oops", |
| 108 |
" ", |
| 109 |
" warnings.warn(pytest.PytestUnhandledThreadExceptionWarning(msg))", |
| 110 |
] |
| 111 |
) |
| 112 |
|
| 113 |
|
| 114 |
@pytest.mark.filterwarnings("error::pytest.PytestUnhandledThreadExceptionWarning") |
| 115 |
def test_unhandled_thread_exception_warning_error(pytester: Pytester) -> None: |
| 116 |
pytester.makepyfile( |
| 117 |
test_it=""" |
| 118 |
import threading |
| 119 |
import pytest |
| 120 |
|
| 121 |
def test_it(): |
| 122 |
def oops(): |
| 123 |
raise ValueError("Oops") |
| 124 |
t = threading.Thread(target=oops, name="MyThread") |
| 125 |
t.start() |
| 126 |
t.join() |
| 127 |
|
| 128 |
def test_2(): pass |
| 129 |
""" |
| 130 |
) |
| 131 |
result = pytester.runpytest() |
| 132 |
assert result.ret == pytest.ExitCode.TESTS_FAILED |
| 133 |
result.assert_outcomes(passed=1, failed=1) |
| 134 |
|
| 135 |
|
| 136 |
@pytest.mark.filterwarnings("error::pytest.PytestUnhandledThreadExceptionWarning") |
| 137 |
def test_threadexception_warning_multiple_errors(pytester: Pytester) -> None: |
| 138 |
pytester.makepyfile( |
| 139 |
test_it=""" |
| 140 |
import threading |
| 141 |
|
| 142 |
def test_it(): |
| 143 |
def oops(): |
| 144 |
raise ValueError("Oops") |
| 145 |
|
| 146 |
t = threading.Thread(target=oops, name="MyThread") |
| 147 |
t.start() |
| 148 |
t.join() |
| 149 |
|
| 150 |
t = threading.Thread(target=oops, name="MyThread2") |
| 151 |
t.start() |
| 152 |
t.join() |
| 153 |
|
| 154 |
def test_2(): pass |
| 155 |
""" |
| 156 |
) |
| 157 |
result = pytester.runpytest() |
| 158 |
assert result.ret == pytest.ExitCode.TESTS_FAILED |
| 159 |
result.assert_outcomes(passed=1, failed=1) |
| 160 |
result.stdout.fnmatch_lines( |
| 161 |
[" | *ExceptionGroup: multiple thread exception warnings (2 sub-exceptions)"] |
| 162 |
) |
| 163 |
|
| 164 |
|
| 165 |
def test_unraisable_collection_failure(pytester: Pytester) -> None: |
| 166 |
pytester.makepyfile( |
| 167 |
test_it=""" |
| 168 |
import threading |
| 169 |
|
| 170 |
class Thread(threading.Thread): |
| 171 |
@property |
| 172 |
def name(self): |
| 173 |
raise RuntimeError("oops!") |
| 174 |
|
| 175 |
def test_it(): |
| 176 |
def oops(): |
| 177 |
raise ValueError("Oops") |
| 178 |
|
| 179 |
t = Thread(target=oops, name="MyThread") |
| 180 |
t.start() |
| 181 |
t.join() |
| 182 |
|
| 183 |
def test_2(): pass |
| 184 |
""" |
| 185 |
) |
| 186 |
|
| 187 |
result = pytester.runpytest() |
| 188 |
assert result.ret == 1 |
| 189 |
result.assert_outcomes(passed=1, failed=1) |
| 190 |
result.stdout.fnmatch_lines( |
| 191 |
["E RuntimeError: Failed to process thread exception"] |
| 192 |
) |
| 193 |
|
| 194 |
|
| 195 |
def test_unhandled_thread_exception_after_teardown(pytester: Pytester) -> None: |
| 196 |
pytester.makepyfile( |
| 197 |
test_it=""" |
| 198 |
import threading |
| 199 |
import pytest |
| 200 |
|
| 201 |
def thread(): |
| 202 |
def oops(): |
| 203 |
raise ValueError("Oops") |
| 204 |
|
| 205 |
t = threading.Thread(target=oops, name="MyThread") |
| 206 |
t.start() |
| 207 |
t.join() |
| 208 |
|
| 209 |
def test_it(request): |
| 210 |
request.config.add_cleanup(thread) |
| 211 |
""" |
| 212 |
) |
| 213 |
|
| 214 |
result = pytester.runpytest("-Werror") |
| 215 |
|
| 216 |
# TODO: should be a test failure or error |
| 217 |
assert result.ret == pytest.ExitCode.INTERNAL_ERROR |
| 218 |
|
| 219 |
result.assert_outcomes(passed=1) |
| 220 |
result.stderr.fnmatch_lines("ValueError: Oops") |
| 221 |
|
| 222 |
|
| 223 |
@pytest.mark.filterwarnings("error::pytest.PytestUnhandledThreadExceptionWarning") |
| 224 |
def test_possibly_none_excinfo(pytester: Pytester) -> None: |
| 225 |
pytester.makepyfile( |
| 226 |
test_it=""" |
| 227 |
import threading |
| 228 |
import types |
| 229 |
|
| 230 |
def test_it(): |
| 231 |
threading.excepthook( |
| 232 |
types.SimpleNamespace( |
| 233 |
exc_type=RuntimeError, |
| 234 |
exc_value=None, |
| 235 |
exc_traceback=None, |
| 236 |
thread=None, |
| 237 |
) |
| 238 |
) |
| 239 |
""" |
| 240 |
) |
| 241 |
|
| 242 |
result = pytester.runpytest() |
| 243 |
|
| 244 |
# TODO: should be a test failure or error |
| 245 |
assert result.ret == pytest.ExitCode.TESTS_FAILED |
| 246 |
|
| 247 |
result.assert_outcomes(failed=1) |
| 248 |
result.stdout.fnmatch_lines( |
| 249 |
[ |
| 250 |
"E pytest.PytestUnhandledThreadExceptionWarning:" |
| 251 |
" Exception in thread <unknown>", |
| 252 |
"E ", |
| 253 |
"E NoneType: None", |
| 254 |
] |
| 255 |
) |