Martin Del Vecchio
Martin Del Vecchio

Reputation: 3848

Where can I find captured stdout for a py.test test that passes?

I am using the py.test reporting hooks (pytest_runtest_makereport() and pytest_report_teststatus()).

When a py.test test fails, I can find the captured stdout data in the report hook (at report.sections[]).

When a py.test test passes, the report.sections[] list is empty.

Where can I find the captured stdout for a test that passes?

Thanks.

Edit: From the source (_pytest/capture.py), it looks like this is available only if the test doesn't pass:

def pytest_runtest_makereport(self, __multicall__, item, call):
    ...
    if not rep.passed:
        addouterr(rep, outerr)

Upvotes: 4

Views: 1606

Answers (1)

Martin Del Vecchio
Martin Del Vecchio

Reputation: 3848

It turns out that the information is available in item.outerr, which is a tuple of two Unicode strings; the first is stdout, and the second is stderr.

Note that py.test specifies these strings in the setup, call, and teardown reports, and that some of those could be empty strings. So in order to save the output, without overwriting it with an empty string, the logic needs to be:

stdout = item.outerr[0]
if stdout and len(stdout):
    whatever.stdout = stdout

stderr = item.outerr[1]
if stderr and len(stderr):
    whatever.stderr = stderr

Upvotes: 2

Related Questions