如何在pytest中打印到控制台? [英] How to print to console in pytest?

查看:103
本文介绍了如何在pytest中打印到控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 TDD(测试驱动开发)与 pytest 结合使用.当我使用 print 时,pytest 不会 print 到控制台.

我正在使用 pytest my_tests.py 来运行它.

documentation 似乎说它应该在默认情况下工作:http:///pytest.org/latest/capture.html

但是:

import myapplication as tum类 TestBlogger:@类方法def setup_class(self):self.user = "爱丽丝"self.b = tum.Blogger(self.user)print "这应该被打印,但它不会!"def test_inherit(self):断言 issubclass(tum.Blogger, tum.Site)链接 = self.b.get_links(posts)print len(links) # 这也不会打印.

我的标准输出控制台没有打印任何内容(只有正常进度以及通过/失败的测试数量).

我正在测试的脚本包含打印:

class Blogger(Site):get_links(自我,帖子):print len(posts) # 它不会在测试中打印出来.

unittest 模块中,所有内容默认打印,这正是我所需要的.但是,出于其他原因,我希望使用 pytest.

有谁知道如何显示打印语句?

解决方案

默认情况下,py.test 捕获标准输出的结果,以便它可以控制它如何打印出来.如果不这样做,它会在没有测试打印文本的上下文的情况下喷出大量文本.

但是,如果测试失败,它会在结果报告中包含一个部分,显示在该特定测试中打印的标准输出内容.

例如

def test_good():对于我在范围内(1000):打印(一)def test_bad():打印('这应该失败!')断言假

结果如下:

<预><代码>>>>py.test tmp.py============================ 测试会话开始 ==============================平台达尔文 -- Python 2.7.6 -- py-1.4.20 -- pytest-2.5.2插件:缓存、cov、pep8、xdist收集了 2 件物品文件==================================== 失败 ====================================___________________________________ 测试_坏 ___________________________________def test_bad():打印('这应该失败!')>断言假E 断言假tmp.py:7: 断言错误------------------------------- 捕获的标准输出 --------------------------------这应该失败!====================== 1 次失败,1 次在 0.04 秒内通过 =========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

注意 Captured stdout 部分.

如果您想在执行时查看 print 语句,可以将 -s 标志传递给 py.test.但是,请注意,这有时可能难以解析.

<预><代码>>>>py.test tmp.py -s============================ 测试会话开始 ==============================平台达尔文 -- Python 2.7.6 -- py-1.4.20 -- pytest-2.5.2插件:缓存、cov、pep8、xdist收集了 2 件物品tmp.py 0123... 等等 ...997998999.这应该失败!F==================================== 失败 ====================================___________________________________ 测试_坏 ___________________________________def test_bad():打印('这应该失败!')>断言假E 断言假tmp.py:7: 断言错误====================== 1 次失败,1 次在 0.02 秒内通过 =========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

I'm trying to use TDD (test-driven development) with pytest. pytest will not print to the console when I use print.

I am using pytest my_tests.py to run it.

The documentation seems to say that it should work by default: http://pytest.org/latest/capture.html

But:

import myapplication as tum

class TestBlogger:

    @classmethod
    def setup_class(self):
        self.user = "alice"
        self.b = tum.Blogger(self.user)
        print "This should be printed, but it won't be!"

    def test_inherit(self):
        assert issubclass(tum.Blogger, tum.Site)
        links = self.b.get_links(posts)
        print len(links)   # This won't print either.

Nothing gets printed to my standard output console (just the normal progress and how many many tests passed/failed).

And the script that I'm testing contains print:

class Blogger(Site):
    get_links(self, posts):
        print len(posts)   # It won't get printed in the test.

In unittest module, everything gets printed by default, which is exactly what I need. However, I wish to use pytest for other reasons.

Does anyone know how to make the print statements get shown?

解决方案

By default, py.test captures the result of standard out so that it can control how it prints it out. If it didn't do this, it would spew out a lot of text without the context of what test printed that text.

However, if a test fails, it will include a section in the resulting report that shows what was printed to standard out in that particular test.

For example,

def test_good():
    for i in range(1000):
        print(i)

def test_bad():
    print('this should fail!')
    assert False

Results in the following output:

>>> py.test tmp.py
============================= test session starts ==============================
platform darwin -- Python 2.7.6 -- py-1.4.20 -- pytest-2.5.2
plugins: cache, cov, pep8, xdist
collected 2 items

tmp.py .F

=================================== FAILURES ===================================
___________________________________ test_bad ___________________________________

    def test_bad():
        print('this should fail!')
>       assert False
E       assert False

tmp.py:7: AssertionError
------------------------------- Captured stdout --------------------------------
this should fail!
====================== 1 failed, 1 passed in 0.04 seconds ======================

Note the Captured stdout section.

If you would like to see print statements as they are executed, you can pass the -s flag to py.test. However, note that this can sometimes be difficult to parse.

>>> py.test tmp.py -s
============================= test session starts ==============================
platform darwin -- Python 2.7.6 -- py-1.4.20 -- pytest-2.5.2
plugins: cache, cov, pep8, xdist
collected 2 items

tmp.py 0
1
2
3
... and so on ...
997
998
999
.this should fail!
F

=================================== FAILURES ===================================
___________________________________ test_bad ___________________________________

    def test_bad():
        print('this should fail!')
>       assert False
E       assert False

tmp.py:7: AssertionError
====================== 1 failed, 1 passed in 0.02 seconds ======================

这篇关于如何在pytest中打印到控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆