Last active
May 22, 2024 18:09
-
-
Save ngoldbaum/afb52186582722dd5bd5506e71c811a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
import warnings | |
import threading | |
def raise_warning(): | |
warnings.warn(RuntimeWarning()) | |
def test_pytest_warns(): | |
b = threading.Barrier(2) | |
def catch_warning(): | |
b.wait() | |
with pytest.warns(RuntimeWarning): | |
raise_warning() | |
task1 = threading.Thread(target=catch_warning) | |
task2 = threading.Thread(target=catch_warning) | |
task1.start() | |
task2.start() | |
task1.join() | |
task2.join() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
goldbaum at Nathans-MBP in ~/Documents/test | |
○ pytest test.py -Werror | |
=============================================================== test session starts ================================================================ | |
platform darwin -- Python 3.13.0b1, pytest-8.2.1, pluggy-1.5.0 | |
rootdir: /Users/goldbaum/Documents/test | |
plugins: hypothesis-6.102.4, repeat-0.9.3 | |
collected 1 item | |
test.py F [100%] | |
===================================================================== FAILURES ===================================================================== | |
________________________________________________________________ test_pytest_warns _________________________________________________________________ | |
cls = <class '_pytest.runner.CallInfo'>, func = <function call_and_report.<locals>.<lambda> at 0x233b8964f60>, when = 'call' | |
reraise = (<class '_pytest.outcomes.Exit'>, <class 'KeyboardInterrupt'>) | |
@classmethod | |
def from_call( | |
cls, | |
func: Callable[[], TResult], | |
when: Literal["collect", "setup", "call", "teardown"], | |
reraise: Optional[ | |
Union[Type[BaseException], Tuple[Type[BaseException], ...]] | |
] = None, | |
) -> "CallInfo[TResult]": | |
"""Call func, wrapping the result in a CallInfo. | |
:param func: | |
The function to call. Called without arguments. | |
:param when: | |
The phase in which the function is called. | |
:param reraise: | |
Exception or exceptions that shall propagate if raised by the | |
function, instead of being wrapped in the CallInfo. | |
""" | |
excinfo = None | |
start = timing.time() | |
precise_start = timing.perf_counter() | |
try: | |
> result: Optional[TResult] = func() | |
../../.pyenv/versions/3.13.0b1/lib/python3.13/site-packages/_pytest/runner.py:341: | |
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | |
../../.pyenv/versions/3.13.0b1/lib/python3.13/site-packages/_pytest/runner.py:241: in <lambda> | |
lambda: runtest_hook(item=item, **kwds), when=when, reraise=reraise | |
../../.pyenv/versions/3.13.0b1/lib/python3.13/site-packages/pluggy/_hooks.py:513: in __call__ | |
return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult) | |
../../.pyenv/versions/3.13.0b1/lib/python3.13/site-packages/pluggy/_manager.py:120: in _hookexec | |
return self._inner_hookexec(hook_name, methods, kwargs, firstresult) | |
../../.pyenv/versions/3.13.0b1/lib/python3.13/site-packages/_pytest/threadexception.py:87: in pytest_runtest_call | |
yield from thread_exception_runtest_hook() | |
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | |
def thread_exception_runtest_hook() -> Generator[None, None, None]: | |
with catch_threading_exception() as cm: | |
try: | |
yield | |
finally: | |
if cm.args: | |
thread_name = ( | |
"<unknown>" if cm.args.thread is None else cm.args.thread.name | |
) | |
msg = f"Exception in thread {thread_name}\n\n" | |
msg += "".join( | |
traceback.format_exception( | |
cm.args.exc_type, | |
cm.args.exc_value, | |
cm.args.exc_traceback, | |
) | |
) | |
> warnings.warn(pytest.PytestUnhandledThreadExceptionWarning(msg)) | |
E pytest.PytestUnhandledThreadExceptionWarning: Exception in thread Thread-2 (catch_warning) | |
E | |
E Traceback (most recent call last): | |
E File "/Users/goldbaum/Documents/test/test.py", line 16, in catch_warning | |
E raise_warning() | |
E ~~~~~~~~~~~~~^^ | |
E File "/Users/goldbaum/Documents/test/test.py", line 7, in raise_warning | |
E warnings.warn(RuntimeWarning()) | |
E ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^ | |
E RuntimeWarning | |
E | |
E During handling of the above exception, another exception occurred: | |
E | |
E Traceback (most recent call last): | |
E File "/Users/goldbaum/.pyenv/versions/3.13.0b1/lib/python3.13/threading.py", line 1039, in _bootstrap_inner | |
E self.run() | |
E ~~~~~~~~^^ | |
E File "/Users/goldbaum/.pyenv/versions/3.13.0b1/lib/python3.13/threading.py", line 990, in run | |
E self._target(*self._args, **self._kwargs) | |
E ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
E File "/Users/goldbaum/Documents/test/test.py", line 15, in catch_warning | |
E with pytest.warns(RuntimeWarning): | |
E raise_warning() | |
E File "/Users/goldbaum/.pyenv/versions/3.13.0b1/lib/python3.13/site-packages/_pytest/recwarn.py", line 318, in __exit__ | |
E fail( | |
E ~~~~^ | |
E f"DID NOT WARN. No warnings of type {self.expected_warning} were emitted.\n" | |
E ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
E f" Emitted warnings: {found_str()}." | |
E ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
E ) | |
E ^ | |
E File "/Users/goldbaum/.pyenv/versions/3.13.0b1/lib/python3.13/site-packages/_pytest/outcomes.py", line 177, in fail | |
E raise Failed(msg=reason, pytrace=pytrace) | |
E Failed: DID NOT WARN. No warnings of type (<class 'RuntimeWarning'>,) were emitted. | |
E Emitted warnings: []. | |
../../.pyenv/versions/3.13.0b1/lib/python3.13/site-packages/_pytest/threadexception.py:77: PytestUnhandledThreadExceptionWarning | |
============================================================= short test summary info ============================================================== | |
FAILED test.py::test_pytest_warns - pytest.PytestUnhandledThreadExceptionWarning: Exception in thread Thread-2 (catch_warning) | |
================================================================ 1 failed in 0.14s ================================================================= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment