Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest capsys: checking output AND getting it reported?

Tags:

python

pytest

Python 3.4.1, pytest 2.6.2.

When a test fails, pytest will routinely report what was printed to stdout by the test. For instance this code:

def method_under_test():
    print("Hallo, Welt!")
    return 41

def test_result_only():
    result = method_under_test()
    assert result == 42

when executed as python -m pytest myfile.py, will report 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 call -----------------------------
Hallo, Welt!
========================== 1 failed in 0.03 seconds ===========================

This is a very nice feature. But when I use pytest's built-in capsys fixture, like this:

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

the report no longer contains the actual output:

================================== FAILURES ===================================
___________________________ test_result_and_stdout ____________________________

capsys = <_pytest.capture.CaptureFixture object at 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 at 0x000000000330C3B0>('Hello')
E        +  where <built-in method startswith of str object at 0x000000000330C3B0> = 'Hallo, Welt!\n'.startswith

pytestest.py:14: AssertionError
========================== 1 failed in 0.03 seconds ===========================

I am not sure whether this behavior is according to specification; the pytest documentation says about readouterr: "After the test function finishes the original streams will be restored."

I have tried assuming capsys is a context manager and have called capsys.__exit__() just before the asserts. This would be an ugly solution, but at least a solution if it restored the output before my assertion. However, this only produces

AttributeError: 'CaptureFixture' object has no attribute '__exit__'

Next I looked into the CaptureFixture class source code and found a promising-looking method close (which calls some pop_outerr_to_orig() method), but calling capsys.close() in my test did not help either, it had no obvious effect at all.

How can I get pytest to report my outputs upon failure in a test using capsys?

like image 290
Lutz Prechelt Avatar asked Oct 25 '14 11:10

Lutz Prechelt


People also ask

Where does pytest print to?

By default, pytest captures the output to sys. stdout/stderr and redirects it to a temporary file.

Which command can be used to show all available fixtures?

Additionally, if you wish to display a list of fixtures for each test, try the --fixtures-per-test flag.

Can we use print in pytest?

Allows to print extra content onto the PyTest reporting. This can be used for example to report sub-steps for long running tests, or to print debug information in your tests when you cannot debug the code.


1 Answers

You're seeing the correct behaviour, when using capsys.readouterr() you're consuming the captured output. Hence any output to stdout and stderr will no longer show up in the test report. But any new output which you create after this and do not consume will still be reported, so you can get the full output back in the report by simply writing it to the output streams once 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
like image 160
flub Avatar answered Sep 23 '22 06:09

flub