运行带有警告作为错误的鼻子测试? [英] Run nosetests with warnings as errors?
问题描述
从命令行运行nosetests
时,如何指定将不可忽略"的警告视为错误?
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
由于我们不希望我们的代码生成警告,所以我不希望这种情况出现在OK
.
As we don't want our code to generate warnings, I don't want this situation to be OK
.
谢谢!
修改:
理想情况下,我想要的是一个鼻子测试命令行选项,该选项在每次测试之前发出warnings.simplefilter('error')
(然后将其清除).
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).
任何涉及在测试代码中使用warnings
模块的解决方案似乎都无法解决问题.我不想手动编辑每个测试模块以将警告转换为错误.另外,我不希望每个测试模块的作者都能忘记打开"警告错误.
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.
推荐答案
nosetests
是一个小的Python脚本.使用编辑器将其打开,并在第一行末尾添加-W error
.这告诉Python解释器将警告转换为异常.
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.
更简单的方法是使用Python环境变量注入将处理警告作为错误"标志:
Even simpler is to use Python environment variable to inject "treat warnings as errors" flag:
PYTHONWARNINGS=error nosetests test/test_*.py --pdb
这篇关于运行带有警告作为错误的鼻子测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!