pytest-dev / pytest-dev/pytest

Add a public method to retrieve output from "terminalreporter" as a string

Open
#8,175 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: reporting type: proposal
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

Hi!

What's the problem this feature will solve?

I would like to be able to easily retrieve the entire test report as a string.

I know there exists plugins like pytest-html or pytest-junitxml. However, I much prefer the default terminal output:

  • it is perfectly detailed and contains all information needed
  • everybody is familiar with the format of this tests report
  • it can be read as "raw" in any text editor
Describe the solution you'd like

Possibly adding a method to the TerminalReporter allowing to get the generated report.

The final goal is to be able to send the complete results by email at the end of the tests for example.

Alternative Solutions

It's possible to redirect output of pytest command but that would prevent to implement pytest-email-reporter as a plugin for example.

The solution I currently use is based on the pastebin plugin which is actually part of the pytest repo: https://github.com/pytest-dev/pytest/blob/89dcfbf293802b6efacc5aea2893f9fec03787bc/src/_pytest/pastebin.py#L31-L49

As you see it requires first to access the protected _tw attribute of the "terminalreporter" plugin and then wrapping its .write() method.

It's working very well but I feel uncomfortable "hacking" pytest internals like this. An official API that would permit to access the session report during pytest_terminal_summary() or pytest_unconfigure() would be perfect.

The drawback is that it would consume more memory since the generated string has to be stored somewhere. Maybe it needs to be configurable. Another possibility is to provide a method for formatting the report, but this seems to require more work.

Additional context

Depending on what you decide, I can implement the solution and open the corresponding PR.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/_pytest/pastebin.py, especially the handling of the terminalreporter plugin and its protected _tw attribute. Read the TerminalReporter entry points used during pytest_terminal_summary() and pytest_unconfigure() to understand when output is available. Done means a documented public API can retrieve the complete terminal report for plugins without wrapping internal methods, with coverage for the relevant lifecycle hooks.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.