pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
11.87k stars 2.65k forks source link

Fine grain output verbosity #11387

Open plannigan opened 1 year ago

plannigan commented 1 year ago

What's the problem this feature will solve?

There are multiple open issues that are requesting more control over the output displayed by pytest. --verbose turns everything up, but it would be nice to be able to just increase or decrease the verbosity for specific aspects of the output.

Open issues that I've found that fit into this problem space (there may be others):

Describe the solution you'd like

I think a way that could resolve all of these is to add an abstraction layer on top config.option.verbose. Rough pseudo-code example:

class OutputVerbosity:
  def __init__(self, config_options):
     self._options = config_options
     self.verbose = self._options.verbose

  def verbosity_for(self, output_type):
     return getattr(self._options, output_type, self.verbose)

For general cases, the verbose attribute could be used. For special cases, an output type name could be passed in retrieve the more fine grained setting. If the specific config is not set, it will default to the normal verbose level. Once this is in place, it would be easier to add more cases in the future if they come up.

Implementation

plannigan commented 1 year ago

Started working on this and unsurprisingly realized that how config.options.verbose is used currently is a bit complicated. So I took the time to see all the places that it is used. The following was based off 6c2feb75d (current head of main):

General Summary

The vast majority of uses of the verbose setting are within _pytest.terminal. In addition to using it directly, TerminalReporter also re-exposes it as a property. The pytest code base does not use the property outside of the TerminalReporter class.

Uses of config.options.verbose outside of TerminalReporter

Uses of config.getoption("verbose")

Uses of config.options.verbose in TerminalReporter

Uses of the TerminalReporter.verbosity property

A read-only property exposed by TerminalReporter that exposes config.options.verbose

Others

plannigan commented 12 months ago

That reference is helpful to understand what things accessing verbosity and how. But it's not the easiest to understand the full behavior. So I went through and documented the behavior by verbosity level. (This helped me get a better understanding of specific aspects that can be controlled.)

-2

-1

0

1

2

plannigan commented 11 months ago

I completely missed assertions.util on the first pass (oops). I updated the previous comments with the missing details.

The-Compiler commented 1 week ago

Should this really have been closed, since (from what I can gather) only a small part of the proposal is implemented so far?

nicoddemus commented 1 week ago

True!