我试着在pytest中使用TDD(测试驱动开发)。
当我使用print时,pytest将不会打印到控制台。
我使用pytest my_tests.py来运行它。
文档似乎说默认情况下它应该工作: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.
我的标准输出控制台没有输出任何内容(只有正常的进度以及通过/失败的测试数量)。
我正在测试的脚本包含打印:
class Blogger(Site):
get_links(self, posts):
print len(posts) # It won't get printed in the test.
在unittest模块中,默认情况下打印所有内容,这正是我所需要的。但是,出于其他原因,我希望使用pytest。
有人知道如何显示打印语句吗?
我需要打印关于跳过测试的重要警告,正好当PyTest静音字面上的一切。
我不想通过测试来传递一个信号,所以我做了如下修改:
def test_2_YellAboutBrokenAndMutedTests():
import atexit
def report():
print C_patch.tidy_text("""
In silent mode PyTest breaks low level stream structure I work with, so
I cannot test if my functionality work fine. I skipped corresponding tests.
Run `py.test -s` to make sure everything is tested.""")
if sys.stdout != sys.__stdout__:
atexit.register(report)
atexit模块允许我在PyTest释放输出流之后打印内容。输出如下所示:
============================= test session starts ==============================
platform linux2 -- Python 2.7.3, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /media/Storage/henaro/smyth/Alchemist2-git/sources/C_patch, inifile:
collected 15 items
test_C_patch.py .....ssss....s.
===================== 10 passed, 5 skipped in 0.15 seconds =====================
In silent mode PyTest breaks low level stream structure I work with, so
I cannot test if my functionality work fine. I skipped corresponding tests.
Run `py.test -s` to make sure everything is tested.
~/.../sources/C_patch$
Message即使在PyTest处于静默模式时也会被打印,如果使用py运行东西则不会被打印。Test -s,所有东西都已经测试好了。
默认为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 ======================
注意俘获标准输出部分。
如果您希望在执行语句时看到打印语句,可以将-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 ======================
使用-s选项将打印所有函数的输出,这可能太多了。
如果你需要特定的输出,你提到的文档页面提供了一些建议:
Insert assert False, "dumb assert to make PyTest print my stuff" at the end of your function, and you will see your output due to failed test.
You have special object passed to you by PyTest, and you can write the output into a file to inspect it later, like
def test_good1(capsys):
for i in range(5):
print i
out, err = capsys.readouterr()
open("err.txt", "w").write(err)
open("out.txt", "w").write(out)
You can open the out and err files in a separate tab and let editor automatically refresh it for you, or do a simple py.test; cat out.txt shell command to run your test.
这是一种相当粗鄙的做事方式,但它可能是你需要的东西:毕竟,TDD意味着你把东西弄得乱七八糟,然后在它准备好时让它干净安静:-)。