[英]pytest capturing all output to stdout
I test the 'current line' that's printed like this 我测试了这样打印的“当前行”
def test_clear(capsys):
out = capsys.readouterr()
outputs_more_than_one_line()
assert out.out == 'last line printed'
# impossible to check previously printed lines?
However, I would like to check everything that was printed. 但是,我想检查所有打印的内容。 I've considered monkeypatching
builtins.print
, but that doesn't seem robust (doesn't capture sys.write.stdout). 我考虑过的Monkeypatching
builtins.print
,但似乎并没有强大的(不捕获sys.write.stdout)。 Is there anyway this would be possible? 无论如何这有可能吗?
The
readouterr()
call snapshots the output so far - and capturing will be continued.readouterr()
调用到目前为止对输出进行快照 - 并且将继续捕获。
therefore you should call readouterr
after print lines, not before: 因此你应该在打印行之后调用
readouterr
,而不是之前:
def test_cap(capsys):
for _ in range(2):
print('outputs_more_than_one_line')
out = capsys.readouterr()
assert out.out != 'last line printed'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.