11

Up to now we call py.test via Jenkins.

If a test fails, we see the usual stacktrace like this

Traceback (most recent call last):
  File "/home/u/src/foo/bar/tests/test_x.py", line 36, in test_schema_migrations
    errors, out))
AssertionError: Unknown output: ["Migrations for 'blue':", ...] 

It would be really great, if I could see local variables like in the django debug page (See https://djangobook.com/wp-content/uploads/figure2_3a.png).

.... But they should only be visible if I want to see them. I guess this means I need a different format than text. Maybe HTML?

Is there a way to enable this?

I never used the tool Sentry. But AFAIK this can display nice tracebacks with local variables.

guettli
  • 25,042
  • 81
  • 346
  • 663
  • moidule `cgitb` can help - insert `cgitb.enable(format='text', context=12)` at the top of the module – Charles Pehlivanian Dec 06 '17 at 12:40
  • @CharlesPehlivanian yes this could work. But this would bloat the default impression. I want to see the local variables only sometimes. If I see them always, then daily work gets a bit harder ... – guettli Dec 06 '17 at 13:31
  • Like using a dropdown as on the djangobook page? Plain text won't do it, don't know what service like that exists... – Charles Pehlivanian Dec 06 '17 at 13:47
  • @CharlesPehlivanian you call it "service". I think you need two parts: One part which creates a traceback with more detailed information and a second part which renders this as HTML. – guettli Dec 06 '17 at 13:51
  • have not tried this, but `cgitb.enable(format='html')` may give you what you want. Give it a try. – Charles Pehlivanian Dec 06 '17 at 14:10
  • @georgexsh `cgitb.enable(format='html')` is a good idea. – guettli Dec 14 '17 at 20:27
  • @guettli I guess what you desired is kind of UX optimization when Jenkins display traceback: collapse local variables part by default, expand on demand. – georgexsh Dec 23 '17 at 20:33
  • @guettli maybe you could run tests with `pytest -l`, then generate two versions of the test report: one with locals, another one filter out locals port, access as you want. but unfortunately, I dont have Jenkins at hand cant test this idea. – georgexsh Dec 23 '17 at 20:42
  • @georgexsh Jenkins is scriptable, you idea (create two versions of the test report) would work. – guettli Dec 23 '17 at 22:16
  • @guettli hope this is useful to you. ps: have answered quite a few of your questions recently :) – georgexsh Dec 24 '17 at 15:33

1 Answers1

10

Use -l/--showlocals option:

pytest --showlocals # show local variables in tracebacks
pytest -l           # show local variables (shortcut)

example:

    def foo():
        a = 1
>       assert 0
E       assert 0

a          = 1

test_foo.py:8: AssertionError

see more details in the doc.

georgexsh
  • 15,984
  • 2
  • 37
  • 62