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.
nose can be integrated with DocTest by using with-doctest option in athe bove command line. The result will be true if the test run is successful, or false if it fails or raises an uncaught exception. nose supports fixtures (setup and teardown methods) at the package, module, class, and test level.
The nose. tools module provides a number of testing aids that you may find useful, including decorators for restricting test execution time and testing for exceptions, and all of the same assertX methods found in unittest.
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
The answer by @khinsen helps a lot, but makes the execution of nosetests stop, if it issues the following warning during test discovery (which is otherwise not visible to the user): "ImportWarning: Not importing directory 'XXX': missing __init__.py
Furthermore, warnings raised during the import of a module (as opposed to warnings raised during a test) should not be treated as errors.
I followed @dbw's advice in writing a plugin, which can be found a github: https://github.com/Bernhard10/WarnAsError
Next to the configure
and options
functions, the plugin implements prepareTestRunner
, where it replaces the default TestRunner by a class which has a different run method:
def prepareTestRunner(self, runner):
return WaETestRunner(runner)
This class stores the original TestRunner and its run
-Method calls the original TestRunner's run method with a different warnings.simplefilter
.
class WaETestRunner(object):
def __init__(self, runner):
self.runner=runner
def run(self, test):
with warnings.catch_warnings():
warnings.simplefilter("error")
return self.runner.run(test)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With