如何在py.test中显示警告

Seb*_*zny 21 python testing django pytest pytest-django

我只是运行py.test我的代码并得到以下输出:

================== 6 passed, 2 pytest-warnings in 40.79 seconds =======================
Run Code Online (Sandbox Code Playgroud)

但是,我看不出有什么py.test想提醒我的.如何打开警告输出到控制台?

py.test --help给我--strict一面旗帜:

- 严格模式下运行pytest,警告成为错误.

但是我只是想看看输出,而不是让我的测试失败.

我检查了pytest.org这个问题,但他们只关心在python中声明警告,而不是在命令行上显示警告.

sas*_*shk 25

在这种情况下,pytest-warnings是为pytest和/或它的插件生成的警告.没有为您的代码生成这些警告.要在报告中列出它们,您需要使用选项-r w.以下是部分py.test --help:

-r chars              show extra test summary info as specified by chars (f)ailed,
                      (E)error, (s)skipped, (x)failed, (X)passed
                      (w)pytest-warnings (a)all.
Run Code Online (Sandbox Code Playgroud)

这将允许在报告中显示警告(记录的顶部)将列出哪些pytest插件使用弃用的参数(在我的情况下):

...
================================ pytest-warning summary ================================ 
WI1 /Projects/.tox/py27/lib/python2.7/site-packages/pytest_timeout.py:68 'pytest_runtest_protocol' hook uses deprecated __multicall__ argument
WI1 /Projects/.tox/py27/lib/python2.7/site-packages/pytest_capturelog.py:171 'pytest_runtest_makereport' hook uses deprecated __multicall__ argument
...
Run Code Online (Sandbox Code Playgroud)

  • 实际上并非如此,那些总是警告,而不是为一些代码生成的警告.我有一个例子,我在我的测试类中添加了一个`__init__`构造函数,导致`py.test`无法使用测试.警告没有显示给我,即使是`--strict`,但你的回答帮助我让`py.test`显示警告,其原因仍在我自己的代码中. (3认同)