Asserting repeated warning does not work in Python 2.7
See original GitHub issueRunning the following test code with Python 2.7 will fail because for some reason the warning is not emitted a second time:
import warnings
import pytest
def warn():
warnings.warn(UserWarning("test"))
def test_warning():
warn()
with pytest.warns(UserWarning):
warn()
The output:
============================= test session starts ==============================
platform linux2 -- Python 2.7.13, pytest-3.2.3, py-1.4.34, pluggy-0.4.0
rootdir: /home/jgosmann, inifile:
collected 1 item
test2.py F
=================================== FAILURES ===================================
_________________________________ test_warning _________________________________
def test_warning():
warn()
with pytest.warns(UserWarning):
> warn()
E Failed: DID NOT WARN. No warnings of type (<type 'exceptions.UserWarning'>,) was emitted. The list of emitted warnings is: [].
test2.py:13: Failed
=============================== warnings summary ===============================
test2.py::test_warning
/home/jgosmann/test2.py:7: UserWarning: test
warnings.warn(UserWarning("test"))
-- Docs: http://doc.pytest.org/en/latest/warnings.html
===================== 1 failed, 1 warnings in 0.01 seconds =====================
Running with Python 3.6.1 succeeds:
============================= test session starts ==============================
platform linux -- Python 3.6.1, pytest-3.2.3, py-1.4.34, pluggy-0.4.0
rootdir: /home/jgosmann, inifile:
collected 1 item
test2.py .
=============================== warnings summary ===============================
test2.py::test_warning
/home/jgosmann/test2.py:7: UserWarning: test
warnings.warn(UserWarning("test"))
-- Docs: http://doc.pytest.org/en/latest/warnings.html
===================== 1 passed, 1 warnings in 0.00 seconds =====================
Note that this also occurs across different tests, e.g. when parametrizing:
@pytest.mark.parametrize('i', range(2))
def test_warning(i):
if i % 2 == 0:
warn()
else:
with pytest.warns(UserWarning):
warn()
When running tests with pytest-xdist
in non-deterministic order this can cause random test failures with Python 2.7.
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (5 by maintainers)
Top Results From Across the Web
warnings — Warning control — Python 3.11.1 documentation
The warnings filter controls whether warnings are ignored, displayed, or turned into errors (raising an exception). Conceptually, the warnings filter maintains ...
Read more >Python 2.7 Unittest check if warning is logged - Stack Overflow
I know the reason is because it doesn't use warnings module. I just wanted to show what i want it to do. The...
Read more >Warning control — Python 2.7.2 documentation - Read the Docs
Python programmers issue warnings by calling the warn() function defined in this module. (C programmers use PyErr_WarnEx(); see Exception Handling for details) ...
Read more >Python's assert: Debug and Test Your Code Like a Pro
In this tutorial, you'll learn how to use Python's assert statement to document, debug, and test code in development.
Read more >Google Python Style Guide
Make sure you run pylint on your code. Suppress warnings if they are inappropriate so that other issues are not hidden. To suppress...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This problem also occurs for non-deprecation warnings. Maybe
pytest.warns
should capture warnings in the same way aspytest.deprecated_call
?via #4104