运行 Django 测试时如何查看标准输出?
当我使用 ./manage.py test
运行测试时,我通过 print
发送到标准输出的任何内容都不会显示。 当测试失败时,我会看到每个失败的测试都有一个“stdout”块,所以我猜 Django 会捕获它(但在测试通过时不会显示它)。
When I run tests with ./manage.py test
, whatever I send to the standard output through print
doesn't show. When tests fail, I see an "stdout" block per failed test, so I guess Django traps it (but doesn't show it when tests pass).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,这个问题是由
NoseTestSuiteRunner
引起的。 添加-- -s
很棘手,而且不是最好的解决方案。尝试在
settings.py
中添加以下行:这解决了我的问题。
Yeah, this issue is caused by
NoseTestSuiteRunner
. Adding-- -s
is tricky and not the best solution.Try to add the following lines in the
settings.py
:This solved my problems.
有几个级别的详细程度会影响我们看到的细节数量:
您可以尝试:
其他可用级别有:
-v 0
:最少的详细信息-v 1
:默认-v 2
:更多详细信息,例如包含打印语句。There are several levels of verbosity available which affects the amount of details we see:
You can try:
Other levels available are:
-v 0
: Least amount of details-v 1
: Default-v 2
: More details e.g. print statements included.检查了
settings.py
中的TEST_RUNNER
,它使用了一个特定于项目的运行程序来调用 Nose。 Nose 有-s
选项来阻止它捕获stdout
,但如果我运行:./manage.py test -s
manage.py
首先捕获它并抛出“没有这样的选项”错误。manage.py
的帮助没有提到这一点,但我发现如果我运行:./manage.py test -- -s
它会忽略
-s
并让我在自定义跑步者一侧捕获它,将其毫无问题地传递给 Nose。Checked
TEST_RUNNER
insettings.py
, it's using a project-specific runner that calls out to Nose. Nose has the-s
option to stop it from capturingstdout
, but if I run:./manage.py test -s
manage.py
captures it first and throws a "no such option" error. The help formanage.py
doesn't mention this, but I found that if I run:./manage.py test -- -s
it ignores the
-s
and lets me capture it on the custom runner's side, passing it to Nose without a problem.使用所有相关软件包的当前版本(
Django==1.11.2
、django-nose==1.4.5
和nose==1.3.7
code>),在运行测试时添加--nocapture
标志就足够了。 因此一个简单的就足够了。
当然,您
的
settings.py
中有Using current versions of all the relevant packages (
Django==1.11.2
,django-nose==1.4.5
andnose==1.3.7
) it is sufficient to add the--nocapture
flag when running your tests. Thus a simplewill suffice.
Granted of course that you have
in your
settings.py
您可能有一些中间测试运行程序,例如 Nose,拦截和存储标准输出。 尝试直接运行 Django 测试,或者写入 stderr。
You probably have some intermediate test runner, such as Nose, intercepting and storing stdout. Try either running the Django tests directly, or write to stderr instead.