运行鼻子测试并将警告作为错误?
从命令行运行 nosetests
时,如何指定“不可忽略”警告应被视为错误?
默认情况下,会打印警告,但不计为失败:
[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
。
谢谢!
编辑: 理想情况下,我想要的是在每次测试之前发出一个 warnings.simplefilter('error')
(并在之后将其清除)的 nostests 命令行选项。
任何涉及在测试代码中使用警告模块的解决方案似乎都违背了这一点。我不想手动编辑每个测试模块以将警告转换为错误。另外,我不希望每个测试模块的作者能够忘记“打开”警告错误。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
nosetests
是一个小型 Python 脚本。使用编辑器打开它,并在第一行末尾添加-W error
。这告诉 Python 解释器将警告转换为异常。更简单的是使用 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.Even simpler is to use Python environment variable to inject "treat warnings as errors" flag:
@khinsen 的答案有很大帮助,但如果在测试发现期间发出以下警告(否则用户看不到),则鼻子测试的执行会停止:“ImportWarning:未导入目录'XXX':缺少
__init__.py
此外,导入模块期间引发的警告(与测试期间引发的警告相反)不应被视为错误,
我在编写插件时遵循了@dbw的建议,可以在github: https://github.com/Bernhard10/WarnAsError
鼻子插件 WarnAsError
旁边的 配置
和options
函数,插件实现了prepareTestRunner
,它用一个具有不同运行方法的类替换默认的TestRunner:该类存储原始的TestRunner及其
run
-Method 使用不同的warnings.simplefilter
调用原始 TestRunner 的 run 方法。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
andoptions
functions, the plugin implementsprepareTestRunner
, where it replaces the default TestRunner by a class which has a different run method:This class stores the original TestRunner and its
run
-Method calls the original TestRunner's run method with a differentwarnings.simplefilter
.我不认为鼻子可以直接控制这一点:警告模块在发出警告时不会引发异常。
warnings
模块使您可以控制应将哪些警告作为异常引发。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.