我正在尝试将测试驱动开发( 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.
我的标准输出控制台不会输出任何内容(只有正常的进度和通过/失败的测试数量)。
我测试的脚本包含print:
class Blogger(Site):
get_links(self, posts):
print len(posts) # It won't get printed in the test.
在unittest
模块中,默认情况下会打印所有内容,这正是我所需要的。然而,出于其他原因,我希望使用pytest
。
有人知道如何显示打印语句吗?
发布于 2014-07-08 02:55:14
默认情况下,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 ======================
发布于 2019-05-02 18:49:25
简短的回答
使用-s
选项:
pytest -s
详细答案
来自the docs
在测试执行期间,将捕获发送到stdout和stderr的任何输出。如果测试或设置方法失败,其相应的捕获输出通常会与失败回溯一起显示。
pytest
具有选项--capture=method
,其中method
是每次测试的捕获方法,可以是以下之一:fd
、sys
或no
。pytest
还有一个选项-s
,它是--capture=no
的快捷方式,该选项允许您在控制台中查看打印语句。
pytest --capture=no # show print statements in console
pytest -s # equivalent to previous command
设置捕获方式或禁用捕获
pytest
可以通过两种方式执行捕获:
Python描述符(
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
发布于 2016-08-05 17:23:32
使用-s
选项将打印所有函数的输出,这可能太多了。
如果您需要特定的输出,您提到的文档页面提供了一些建议:
assert False, "dumb assert to make PyTest print my stuff"
,由于测试失败,您将看到您的输出。def test_good1(capsys):对于i输入范围(5):打印i输出,err = capsys.readouterr() open("err.txt","w").write(err) open("out.txt","w").write( out )
您可以在单独的选项卡中打开out
和err
文件,并让编辑器自动刷新它,或者执行一个简单的py.test; cat out.txt
外壳命令来运行测试。
这是一种相当老套的方式,但它可能是你需要的东西:毕竟,TDD意味着你把东西弄乱了,当它准备好的时候就让它干净和安静:-)。
https://stackoverflow.com/questions/24617397
复制相似问题