Run nosetests with warnings as errors?

2019-04-03 23:05发布

问题:

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.

回答1:

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


回答2:

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

A nose plugin 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)


回答3:

I don't think nose can directly control this: the warnings module doesn't raise an exception when the warning is issued. The warnings module gives you control over which warnings should be raised as exceptions.