Fatigue warnings when running unittests

When running unittests, I would like to see failure warnings. It looks like since the Python 2.7 ban warnings are off . I will quote from the page:

For Python 2.7, it was a political decision to disable warnings of interest to developers by default. The deprecated warning and its descendants are now ignored unless requested otherwise, preventing users from seeing the warnings raised by the application. This change was also made in a branch that became Python 3.2. (Discussed on stdlib-sig and was posted in release 7319.)

Later I get the impression that while running the unittests I should see deprecation warnings:

The unittest module also automatically updates deprecation warnings when running tests.

Well .. just put it down, it doesn't work for me, so I must be doing something wrong. I've tested the following code:

import warnings
import unittest

def spam():
    warnings.warn('test', DeprecationWarning, stacklevel=2)
    return 'spam'

class Eggs(object):
    def __init__(self):
        self.spam = spam()

class Test(unittest.TestCase):
    def test_warn(self):
        eggs = Eggs()
        self.assertEqual('spam', eggs.spam)

      

Then I run the code (saved in spam.py

):

python -m 'unittest' spam

      

And this gives me the following output:

.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

      

No deprecation warnings. So the question is: what am I doing wrong here?

+3


source to share


2 answers


Looks like the documentation is wrong - 2.7 unittest does not repeat failure warnings.

>>> import warnings
>>> from pprint import pprint

>>> pprint(warnings.filters)
[('ignore', None, <type 'exceptions.DeprecationWarning'>, None, 0),
 ('ignore', None, <type 'exceptions.PendingDeprecationWarning'>, None, 0),
 ('ignore', None, <type 'exceptions.ImportWarning'>, None, 0),
 ('ignore', None, <type 'exceptions.BytesWarning'>, None, 0)]

>>> import unittest
>>> pprint(warnings.filters)
[('ignore', None, <type 'exceptions.DeprecationWarning'>, None, 0),
 ('ignore', None, <type 'exceptions.PendingDeprecationWarning'>, None, 0),
 ('ignore', None, <type 'exceptions.ImportWarning'>, None, 0),
 ('ignore', None, <type 'exceptions.BytesWarning'>, None, 0)]

      

... and in unittest.py

there is nothing that I have seen that reenables DeprecationWarning

.

You can of course enable them yourself:

warnings.simplefilter('always', DeprecationWarning)

      



Or on the command line:

$ python -Wd -m 'unittest' spam
spam.py:10: DeprecationWarning: test
  self.spam = spam()
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

      

Or with a decorator applied to each of your functions unittest.TestCase

to DeprecationWarning

be included for testing only:

import warnings
import unittest

def enable_DeprecationWarning(fn):
    def _wrapped(*args, **kwargs):
        with warnings.catch_warnings():
            warnings.simplefilter('always', DeprecationWarning)
            return fn(*args, **kwargs)
    return _wrapped

def spam():
    warnings.warn('test', DeprecationWarning, stacklevel=2)
    return 'spam'

class Eggs(object):
    def __init__(self):
        self.spam = spam()

class Test(unittest.TestCase):
    @enable_DeprecationWarning
    def test_warn(self):
        eggs = Eggs()
        self.assertEqual('spam', eggs.spam)

if __name__ == '__main__':
        unittest.main()

      

The command line option is probably best suited for unit testing as it doesn't require any code changes.

+4


source


Since I am using Eclipse for Python development, I decided to use a different solution than suggested. It is also possible to enable warnings using an environment variable. If the environment variable is PYTHONWARNINGS

set to a value default

, crash warnings are displayed.

In Eclipse (PyDev) the interpreter can be changed to include an environment variable. Thus, it is only available for projects using this interpreter.



pydev interpreter config

+1


source







All Articles