Debugging with pdb
pdb,[49] which stands for “Python debugger,” is part of the Python standard library, so we don’t need to install anything to use it. We’ll get pdb up and running and then look at some of the most useful commands within pdb.
You can launch pdb from pytest in a few different ways:
-
Add a breakpoint() call to either test code or application code. When a pytest run hits a breakpoint() function call, it will stop there and launch pdb.
-
Use the --pdb flag. With --pdb, pytest will stop at the point of failure. In our case, that will be at the assert len(the_list) == 2 line.
-
Use the --trace flag. With --trace, pytest will stop at the beginning of each test.
For our purposes, combining --lf and --trace will work perfectly. The combo ...
Get Python Testing with pytest now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.