Python – Run nosetests with warnings as errors

nosepythonunit testingwarnings

When running nosetests from the command line, how do you specify that 'non-ignored' warnings should be treated as errors?

By default, warnings are printed, but not counted as failures:

[snip]/service/accounts/database.py:151: SADeprecationWarning: Use session.add()
  self.session.save(state)
[snip]/service/accounts/database.py:97: SADeprecationWarning: Use session.add()
  self.session.save(user)
............
----------------------------------------------------------------------
Ran 12 tests in 0.085s

OK

As we don't want our code to generate warnings, I don't want this situation to be OK.

Thanks!

Edit:
Ideally what I'd like is a nosetests command line option that issues a warnings.simplefilter('error') prior to each test (and cleans it out afterwards).

Any solution that involves using the warnings module in the test code seems to defeat the point. I don't want to manually edit each test module to transform warnings into errors. Plus I don't want the author of each test module to be able to forget to 'turn on' warning errors.

Best Answer

nosetests is a small Python script. Open it with an editor, and add -W error at the end of the first line. This tells the Python interpreter to convert warnings into exceptions.

Even simpler is to use Python environment variable to inject "treat warnings as errors" flag:

PYTHONWARNINGS=error nosetests test/test_*.py --pdb