I'm using pytest for my test suite. While catching bugs in complex inter-components test, I would like to place import ipdb; ipdb.set_trace()
in the middle of my code to allow me to debug it.
However, since pytest traps sys.stdin/sys.stdout ipdb fails. How can I use ipdb while testing with pytest.
I'm not interested in jumping to pdb or ipdb after a failure, but to place breaks anywhere in the code and be able to debug it there before the failure occurs.
pdb. set_trace (*, header=None) Enter the debugger at the calling stack frame. This is useful to hard-code a breakpoint at a given point in a program, even if the code is not otherwise being debugged (e.g. when an assertion fails). If given, header is printed to the console just before debugging begins.
Using pytest to start pdb debuggerYou can use different command options like l (list), a(args), n(next) etc., after entering into pdb prompt. To quit from the pdb prompt, simply press q(quit) and the ENTER key. This will invoke the Python debugger at the start of every test. ii) Enter into PDB prompt on failures.
To set a breakpoint in your code use the native Python import pdb;pdb. set_trace() call in your code and pytest automatically disables its output capture for that test: Output capture in other tests is not affected. Any prior test output that has already been captured and will be processed as such.
The error is raised because pytest captures output by default.
You can run pytest with -s
option (turn off capture output). For example:
py.test -s my_test.py
and then in my_test.py
:
import ipdb; ipdb.set_trace()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With