运行鼻子测试并将警告作为错误?

发布于 2024-08-10 11:57:05 字数 715 浏览 3 评论 0原文

从命令行运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

咽泪装欢 2024-08-17 11:57:05

nosetests 是一个小型 Python 脚本。使用编辑器打开它,并在第一行末尾添加 -W error 。这告诉 Python 解释器将警告转换为异常。

更简单的是使用 Python 环境变量注入“将警告视为错误”标志:

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

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
不…忘初心 2024-08-17 11:57:05

@khinsen 的答案有很大帮助,但如果在测试发现期间发出以下警告(否则用户看不到),则鼻子测试的执行会停止:“ImportWarning:未导入目录'XXX':缺少 __init__.py

此外,导入模块期间引发的警告(与测试期间引发的警告相反)不应被视为错误,

我在编写插件时遵循了@dbw的建议,可以在github: https://github.com/Bernhard10/WarnAsError

鼻子插件 WarnAsError

旁边的 配置options函数,插件实现了prepareTestRunner,它用一个具有不同运行方法的类替换默认的TestRunner:

def prepareTestRunner(self, runner):
    return WaETestRunner(runner)

该类存储原始的TestRunner及其 run-Method 使用不同的 warnings.simplefilter 调用原始 TestRunner 的 run 方法。

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)

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)
岁月打碎记忆 2024-08-17 11:57:05

我不认为鼻子可以直接控制这一点:警告模块在发出警告时不会引发异常。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文