New issue 439: capsys fixture does not collect the same test output as reported by pytest https://bitbucket.org/hpk42/pytest/issue/439/capsys-fixture-does-not-collect-the-same
Jurko Gospodnetić: I noticed that test fixture output gets reported by pytest as part of the output captured for a particular test. However, when you use the capsys fixture to access all the output collected for that test - test fixture output is not included and instead still gets reported as captured output at the end of the test. You can use the following test to illustrate the problem. Just run it as a part of the internal pytest test suite. ``` import fnmatch def test_consistent_reported_and_capsys_test_output(testdir): """ capsys test fixture should allow collecting complete test output. Running a test with the capsys fixture should allow the test to collect the exact same output as reported by pytest when running a matching test without using capsys. Illustrates a defect in pytest 2.5.0 where capsys does not allow accessing fixture output but still reports that output as part of the test's captured output. """ testdir.makepyfile(r"""\ import pytest @pytest.fixture() def ola(request): print("<DIRECT> in the fixture") def do_the_unga_bunga(capsys=None): print("<DIRECT> in the test") if capsys: out, err = capsys.readouterr() for line in out[:-1].split("\n"): print("<CAPSYS> %s" % (line[9:],)) pytest.fail() def test_direct(ola): do_the_unga_bunga() def test_with_capsys(ola, capsys): do_the_unga_bunga(capsys) """) result = testdir.runpytest("--tb=short", "-k", "test_direct") output = result.stdout.lines direct_lines = fnmatch.filter(output, "<DIRECT>*") capsys_lines = fnmatch.filter(output, "<CAPSYS>*") assert len(direct_lines) == 2 assert len(capsys_lines) == 0 output_direct = [x[9:] for x in direct_lines] result = testdir.runpytest("--tb=short", "-k", "test_with_capsys") output = result.stdout.lines direct_lines = fnmatch.filter(output, "<DIRECT>*") capsys_lines = fnmatch.filter(output, "<CAPSYS>*") assert len(direct_lines) + len(capsys_lines) == 2 output_capsys = [x[9:] for x in capsys_lines] assert output_direct == output_capsys ``` The test can be instrumented with more precise assertions that would fail earlier, but as it stands, it illustrates that exactly the point I'm trying to make here - that the directly captured output and the output captured via capsys do not match. Hope this helps. Best regards, Jurko Gospodnetić _______________________________________________ pytest-commit mailing list pytest-commit@python.org https://mail.python.org/mailman/listinfo/pytest-commit