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?
The easiest way is using the py.test mechanism to create breakpoint
http://pytest.org/latest/usage.html#setting-a-breakpoint-aka-set-trace
Or if you want
pytest
's debugger as a one-liner, change yourimport pdb; pdb.set_trace()
intoimport pytest; pytest.set_trace()
it's real simple: put an
assert 0
where you want to start debugging in your code and run your tests with: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.I found that I can run py.test with capture disabled, then use pdb.set_trace() as usual.
I'm not familiar with py.test, put for unittest, you do the following. Maybe py.test is similar:
In your test module (mytestmodule.py):
Then run the test with
You will get an interactive pdb shell.
Looking at the docs, it looks like py.test has a
--pdb
command line option:http://codespeak.net/py/dist/test/features.html