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). Is there anyway this would be possible?
The doc says:
The
readouterr()
call snapshots the output so far - and capturing will be continued.
therefore you should call readouterr
after print lines, not before:
def test_cap(capsys):
for _ in range(2):
print('outputs_more_than_one_line')
out = capsys.readouterr()
assert out.out != 'last line printed'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.