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
12.14k stars 2.69k forks source link

hide functions not related to the current project in tracebacks #10251

Closed glehmann closed 2 years ago

glehmann commented 2 years ago

What's the problem this feature will solve?

In case of test failure, pytest currently reports the full back trace, excluding pytest's own functions. This way to report the back traces works well when testing the code from a python module, but is not optimal when using an external library in the test.

It is possible to exclude entries from the back traces with __tracebackhide__ = True, but it is an invasive solution for the library that

Describe the solution you'd like

I would much prefer a way to hide the functions that are not part of the currently tested project. We could use the module installation path to find out whether the module is installed or in the project sources. Testing if site-packages is in the module path is probably a bit rough but has worked for us so far!

A --hide-external-trace=yes command line option could be used to activate this feature.

Alternative Solutions

I've proposed a quite ugly monkey patch in the SO posts listed below.

Additional context

A couple of SO questions related to that problem:

https://stackoverflow.com/questions/24402456/py-test-hide-stacktrace-lines-from-unittest-module

https://stackoverflow.com/questions/22912747/py-test-tracebacks-highlight-my-code-fold-frames-of-framework

I would be interested in creating a PR for this feature — just checking if pytest authors might be interested before going further :smile:

RonnyPfannschmidt commented 2 years ago

There is a pretty old issue about filtering frameworks in a sensible way

nicoddemus commented 2 years ago

Yes, thanks @glehmann, here's the original issue: #283.

Closing this as duplicate then. 👍