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:
Asides using "startswith".You can also use the "in" keyword, for example:
assert "Hello" in output
This is great if you have a huge amount of data being passed to stdout, you can use "in" to check for different lines in your stdout.
def test_result_and_stdout(capsys):
result = method_under_test()
out, err = capsys.readouterr()
sys.stdout.write(out)
sys.stderr.write(err)
assert "Hello" in out
assert result == 42
You can also assert what's passed into stderr instead of stdout by using:
assert "What you are expecting" in err
Also note that line:
out, err = capsys.readouterr()
creates a snapshot of the output to both stdout and stderr so far so you can assert what you are expecting for that test.