question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Asserting repeated warning does not work in Python 2.7

See original GitHub issue

Running 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:closed
  • Created 6 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
jgosmanncommented, Jan 9, 2018

This problem also occurs for non-deprecation warnings. Maybe pytest.warns should capture warnings in the same way as pytest.deprecated_call?

0reactions
asottilecommented, Oct 11, 2018

via #4104

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found