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

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

its 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.

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)

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

The easiest way is using the py.test mechanism to create breakpoint

http://pytest.org/latest/usage.html#setting-a-breakpoint-aka-set-trace

import pytest
def test_function():
    ...
    pytest.set_trace()    # invoke PDB debugger and tracing

Or if you want pytests debugger as a one-liner, change your import pdb; pdb.set_trace() into import pytest; pytest.set_trace()

Leave a Reply

Your email address will not be published. Required fields are marked *