9

I'm using pytest with mock in my python project.

When I get a test failure that involves a mock object (almost all of them), the traceback dives into the mock library source code and my code that's actually triggering the failure often scrolls out of view.

Is there a way to tell py.test to exclude installed libraries from the traceback or something like that? The noise level is taking away from what is otherwise a sweet testing library.

scanny
  • 26,423
  • 5
  • 54
  • 80

2 Answers2

7

There is a hack: a function defining the local variable __tracebackhide__ will not be shown. Example:

def some_support_code(x, y):
    __tracebackhide__ = True
    assert x == y
Armin Rigo
  • 12,048
  • 37
  • 48
  • 1
    Hmm, that's interesting. I wonder how I would make that work in my situation, to hide all mock library calls? – scanny Sep 08 '13 at 08:54
  • Add `__tracebackhide__ = True` in the most common functions of your mock library. If you prefer something more general you can always hack py.test directly. – Armin Rigo Sep 08 '13 at 12:43
  • 1
    Ah, but mock is a standard Python library, installed via `pip install mock`, not code I've developed myself. What I'd be looking for is a way to exclude any "library" code that I don't own, perhaps by pytest detecting that it was installed in `site-package/` or whatever and excluding it, i.e. treating it as a "black box", as I do. – scanny Sep 08 '13 at 20:18
3

pytest-mock includes a feature that will hide tracebacks from the mock library. Just install pytest-mock and you're done.

Chronial
  • 66,706
  • 14
  • 93
  • 99