Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute ipdb.set_trace() at will while running pytest tests

Tags:

python

pytest

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.

like image 946
manu Avatar asked Apr 15 '13 19:04

manu


People also ask

What is pdb Set_trace ()?

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.

How do I use pdb pytest?

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.

How do you set a breakpoint in pytest?

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.


1 Answers

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() 
like image 190
petRUShka Avatar answered Sep 21 '22 20:09

petRUShka