pytest.warns is a subclass of the standard library warnings.catch_warnings context manager, which is not thread safe (this is noted in the docs).
If it's not possible for pytest to have a thread-safe version, it would be nice if the docs for pytest.warns could note that it's implemented using warnings.catch_warnings and is not thread safe for the same reason.
pytest 8.2.1 on MacOS Sonoma.
Fails about 10-20% of the time on my machine:
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()
pip listfrom the virtual environment you are usingpytest.warnsis a subclass of the standard librarywarnings.catch_warningscontext manager, which is not thread safe (this is noted in the docs).If it's not possible for pytest to have a thread-safe version, it would be nice if the docs for
pytest.warnscould note that it's implemented usingwarnings.catch_warningsand is not thread safe for the same reason.pytest 8.2.1 on MacOS Sonoma.
Fails about 10-20% of the time on my machine: