Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I debug with python debugger when using py.test somehow?

I am using py.test for unit testing my python program. I wish to debug my test code with the python debugger the normal way (by which I mean pdb.set_trace() in the code) but I can't make it work.

Putting pdb.set_trace() in the code doesn't work (raises IOError: reading from stdin while output is captured). I have also tried running py.test with the option --pdb but that doesn't seem to do the trick if I want to explore what happens before my assertion. It breaks when an assertion fails, and moving on from that line means terminating the program.

Does anyone know a way to get debugging, or is debugging and py.test just not meant to be together?

like image 846
Joel Avatar asked Apr 20 '10 21:04

Joel


People also ask

What is Python debugger used for?

It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame. It also supports post-mortem debugging and can be called under program control.

Is debugging possible in Python?

Python has a built-in debugger called pdb . It's a simple utility with a command line interface that does the main job. It has all the debugger features you'll need, but if you're looking to pimp it up a little, you can extend it using ipdb, which will provide the debugger with features from IPython.


2 Answers

it's real simple: put an assert 0 where you want to start debugging in your code and run your tests with:

py.test --pdb  

done :)

Alternatively, if you are using pytest-2.0.1 or above, there also is the pytest.set_trace() helper which you can put anywhere in your test code. Here are the docs. It will take care to internally disable capturing before sending you to the pdb debugger command-line.

like image 200
hpk42 Avatar answered Oct 05 '22 10:10

hpk42


I found that I can run py.test with capture disabled, then use pdb.set_trace() as usual.

> py.test --capture=no ============================= test session starts ============================== platform linux2 -- Python 2.5.2 -- pytest-1.3.3 test path 1: project/lib/test/test_facet.py  project/lib/test/test_facet.py ...> /home/jaraco/projects/project/lib/functions.py(158)do_something() -> code_about_to_run('') (Pdb) 
like image 35
Jason R. Coombs Avatar answered Oct 05 '22 10:10

Jason R. Coombs