如何在pytest中打印到控制台?

新手上路,请多包涵

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

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

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

但:

 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.

没有任何东西打印到我的标准输出控制台(只是正常的进度和多少测试通过/失败)。

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

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

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

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

原文由 BBedit 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.6k
2 个回答

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

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

例如,

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

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

结果如下:

 >>> 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 ======================

注意 Captured stdout 部分。

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

 >>> 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 ======================

原文由 tbekolay 发布,翻译遵循 CC BY-SA 3.0 许可协议

使用 -s 选项:

 pytest -s

详细解答

文档

在测试执行期间,将捕获发送到 stdoutstderr 的任何输出。如果测试或设置方法失败,其相应的捕获输出通常会与失败回溯一起显示。

pytest has the option --capture=method in which method is per-test capturing method, and could be one of the following: fd , sysnopytest 也有选项 -s 这是 --capture=no 的快捷方式,这是您在控制台中看到打印语句的选项。

 pytest --capture=no     # show print statements in console
pytest -s               # equivalent to previous command

设置捕获方法或禁用捕获

pytest 可以通过两种方式进行抓包:

  1. 文件描述符 (FD) 级别捕获(默认):所有写入操作系统文件描述符 1 和 2 的操作都将被捕获。

  2. sys 级捕获:仅写入 Python 文件 sys.stdout 和 sys.stderr 将被捕获。不捕获对文件描述符的写入。

 pytest -s            # disable all capturing
pytest --capture=sys # replace sys.stdout/stderr with in-mem files
pytest --capture=fd  # also point filedescriptors 1 and 2 to temp file

原文由 lmiguelvargasf 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题