python - pytest capsys: checking output AND getting it reported? -



python - pytest capsys: checking output AND getting it reported? -

python 3.4.1, pytest 2.6.2.

when test fails, pytest routinely study printed stdout test. instance code:

def method_under_test(): print("hallo, welt!") homecoming 41 def test_result_only(): result = method_under_test() assert result == 42

when executed python -m pytest myfile.py, study this:

================================== failures =================================== ______________________________ test_result_only _______________________________ def test_result_only(): result = method_under_test() > assert result == 42 e assert 41 == 42 pytestest.py:9: assertionerror ---------------------------- captured stdout phone call ----------------------------- hallo, welt! ========================== 1 failed in 0.03 seconds ===========================

this nice feature. when utilize pytest's built-in capsys fixture, this:

def test_result_and_stdout(capsys): result = method_under_test() out, err = capsys.readouterr() assert out.startswith("hello") assert result == 42

the study no longer contains actual output:

================================== failures =================================== ___________________________ test_result_and_stdout ____________________________ capsys = <_pytest.capture.capturefixture object @ 0x000000000331fb70> def test_result_and_stdout(capsys): result = method_under_test() out, err = capsys.readouterr() > assert out.startswith("hello") e assert <built-in method startswith of str object @ 0x000000000330c3b0>('hello') e + <built-in method startswith of str object @ 0x000000000330c3b0> = 'hallo, welt!\n'.startswith pytestest.py:14: assertionerror ========================== 1 failed in 0.03 seconds ===========================

i not sure whether behavior according specification; pytest documentation says readouterr: "after test function finishes original streams restored."

i have tried assuming capsys context manager , have called capsys.__exit__() before asserts. ugly solution, @ to the lowest degree solution if restored output before assertion. however, produces

attributeerror: 'capturefixture' object has no attribute '__exit__'

next looked capturefixture class source code , found promising-looking method close (which calls pop_outerr_to_orig() method), calling capsys.close() in test did not help either, had no obvious effect @ all.

how can pytest study outputs upon failure in test using capsys?

you're seeing right behaviour, when using capsys.readouterr() you're consuming captured output. hence output stdout , stderr no longer show in test report. new output create after , not consume still reported, can total output in study writing output streams 1 time more:

def test_result_and_stdout(capsys): result = method_under_test() out, err = capsys.readouterr() sys.stdout.write(out) sys.stderr.write(err) assert out.startswith("hello") assert result == 42

python py.test

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -