PyTest: Interactive Output (instead of pure ASCII)

Viewed 340

How can I get expandable output from PyTest?

For example the local variables. It would be great if I could expand/collapse them depending on my need.

I think that's where the current text-output of PyTest reaches its limit.

The question: Is there a way to get an interactive output from PyTest?

With interactive I mean to be able to expand/collapse the local variables.

expand-on-demand

Above example is from the django debug page.

I would like to have such an interactive output if I run pytest.

My output looks like this:

pytest-output-text-only

3 Answers

For achieving an output similar to the screenshot (which I assume is from a Django debugging session), you will need pytest and the pytest-html plugin.

You invoke pytest using this command line.

pytest --showlocals --html=report.html --self-contained-html testmodule/test.py

This will give you a HTML report with a nice overview of collapsible tests. Passing the argument --showlocals will also be reflected in the HTML report. All failed tests will show the local variable scope.

I created a small example repository with further instructions.

HTML report example

Capturing of the stdout/stderr output

During test execution any output sent to stdout and stderr is captured. If a test or a setup method fails its according captured output will usually be shown along with the failure traceback.

In addition, stdin is set to a “null” object which will fail on attempts to read from it because it is rarely desired to wait for interactive input when running automated tests.

By default capturing is done by intercepting writes to low level file descriptors. This allows to capture output from simple print statements as well as output from a subprocess started by a test.

Welcome to py.test

Related