Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message when pytest.warns fail #2150

Merged

Conversation

lesteve
Copy link
Contributor

@lesteve lesteve commented Dec 20, 2016

The error message contains the expected type of warning and the
warnings that were captured. Add tests.

You could argue that having the stderr is good enough but in my original use case, either the stderr was very noisy or I managed to miss it completely. I reckon it does not hurt to have a clearer error message anyway.

The line of the error message is very long now. Let me know if you would rather have me split it manually or you have better ways to deal with it.

Simple snippet:

import warnings

import pytest


def test():
    with pytest.warns(RuntimeWarning):
        warnings.warn('user', UserWarning)
        warnings.warn('import', ImportWarning)

master:

======================================================== test session starts ========================================================
platform linux -- Python 3.5.2, pytest-3.0.6.dev0, py-1.4.31, pluggy-0.4.0
rootdir: /tmp, inifile: 
plugins: cov-2.3.1
collected 1 items 

../../../../tmp/test_pytest.py F

============================================================= FAILURES ==============================================================
_______________________________________________________________ test ________________________________________________________________

    def test():
        with pytest.warns(RuntimeWarning):
            warnings.warn('user', UserWarning)
>           warnings.warn('import', ImportWarning)
E           Failed: DID NOT WARN

/tmp/test_pytest.py:9: Failed
------------------------------------------------------- Captured stderr call --------------------------------------------------------
/tmp/test_pytest.py:8: UserWarning: user
  warnings.warn('user', UserWarning)
/tmp/test_pytest.py:9: ImportWarning: import
  warnings.warn('import', ImportWarning)
===================================================== 1 failed in 0.02 seconds ======================================================

This PR:

======================================================== test session starts ========================================================
platform linux -- Python 3.5.2, pytest-3.0.6.dev0, py-1.4.31, pluggy-0.4.0
rootdir: /tmp, inifile: 
plugins: cov-2.3.1
collected 1 items 

../../../../tmp/test_pytest.py F

============================================================= FAILURES ==============================================================
_______________________________________________________________ test ________________________________________________________________

    def test():
        with pytest.warns(RuntimeWarning):
            warnings.warn('user', UserWarning)
>           warnings.warn('import', ImportWarning)
E           Failed: DID NOT WARN. No warnings of type (<class 'RuntimeWarning'>,) was emitted. The list of emitted warnings is: [UserWarning('user',), ImportWarning('import',)].

/tmp/test_pytest.py:9: Failed
------------------------------------------------------- Captured stderr call --------------------------------------------------------
/tmp/test_pytest.py:8: UserWarning: user
  warnings.warn('user', UserWarning)
/tmp/test_pytest.py:9: ImportWarning: import
  warnings.warn('import', ImportWarning)
===================================================== 1 failed in 0.02 seconds ======================================================

The error message contains the expected type of warnings and the
warnings that were captured. Add tests.
following contribution guidelines
@coveralls
Copy link

Coverage Status

Coverage remained the same at 92.832% when pulling c5f0b75 on lesteve:add-caught-warnings-info-when-warns-fail into da40bcf on pytest-dev:master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 92.832% when pulling bfada96 on lesteve:add-caught-warnings-info-when-warns-fail into da40bcf on pytest-dev:master.

@decentral1se
Copy link
Contributor

Oh, that seems quite useful. Nice work 👍

@nicoddemus
Copy link
Member

Nice work!

Tests fail due to unrelated failures on the trial environments. Hopefully I will have time to work on it this week.

This was referenced Mar 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants