2

Tracebacks of my tests are too long since I use pytest.

Pytests includes surrounding code lines and a lot of other information.

I like to see this information if the traceback line (frame) is from my code. But I don't want to see it, if it is from a library or framework.

I could not find a way to filter or fold the frames.

Any hints?

Update, 8 years later: I think it is time to say goodbye to ASCII and embrace html. With html you can expand/collapse parts (like in the great django debug view).

Unfortunately there seems to be no pytest output which gives you nice interface like for example sentry does.

neozen
  • 93
  • 7
guettli
  • 23,964
  • 63
  • 293
  • 556

2 Answers2

4

I've got this monkeypatch I'm sticking in my conftest.py when I want to do this (only works with --tb=native):

It's a port of this https://stackoverflow.com/a/24679193/59412 by Jeremy Allen to the latest version of pytest, 6.2.3. It's certainly not the cleanest thing, but it removes all code from 3rd party dependencies (e.g. anything pip installed) from the tracebacks.

# 5c4048fc-ccf1-44ab-a683-78a29c1a98a6
import _pytest._code.code
def should_skip_line(line):
    """
    decide which lines to skip
    """
    return 'site-packages' in line

class PatchedReprEntryNative(_pytest._code.code.ReprEntryNative):
    def __init__(self, tblines):
        self.lines = []
        while len(tblines) > 0:
            # [...yourfilter...]/framework_code.py", line 1, in test_thing
            line = tblines.pop(0)
            if should_skip_line(line):
                # the line of framework code you don't want to see either...
                tblines.pop(0)
            else:
                self.lines.append(line)
_pytest._code.code.ReprEntryNative = PatchedReprEntryNative
del _pytest._code.code
neozen
  • 93
  • 7
1

I'm afraid this is currently not possible and is an enhancement request: https://bitbucket.org/hpk42/pytest/issue/283/traceback-filtering-hook

The only control you have for the moment is --tb=short etc.

flub
  • 5,457
  • 22
  • 23