Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting breakpoints with nosetests --pdb option

nosetests --pdb let's me halt upon error or failure, but this is too late for my needs. Stepping through code during execution helps me debug where the problem is.

However, nosetests are helpful as they allow tests that rely on relative imports (i.e. tests in a package).

How can I set breakpoints before the tests are executed? Currently I'm using:

python -m pdb /path/to/my/nosetests testfile.py 

This solution isn't adequate. Nosetests interfere with pdb output, and my keyboard controls (e.g. arrow keys) are broken.

Using import pdb; pdb.set_trace() would seem like a good idea, however nosetests is blocking my access to the pdb console.

like image 816
Devin Avatar asked Feb 09 '11 21:02

Devin


People also ask

How do you set a breakpoint in pdb?

Optionally, you can also tell pdb to break only when a certain condition is true. Use the command b (break) to set a breakpoint. You can specify a line number or a function name where execution is stopped. If filename: is not specified before the line number lineno , then the current source file is used.

How do I use pdb debugging?

To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally, and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().

How do you set a breakpoint in a Python script?

Just use python -m pdb <your_script>. py then b <line_number> to set the breakpoint at chosen line number (no function parentheses). Hit c to continue to your breakpoint. You can see all your breakpoints using b command by itself.

What is debugging pdb?

Pdb is a powerful tool for finding syntax errors, spelling mistakes, missing code, and other issues in your Python code. Posted: April 18, 2022 | 14 min read | by Jose Vicente Nunez (Sudoer) WOCinTechChat,CCBY2.0.


2 Answers

Even better than remembering to use -s is to use the set_trace variant that comes with Nose. Add

from nose.tools import set_trace; set_trace() 

wherever you'd like to break in to the debugger. The stdin/out redirection will be taken care of for you. The only strange side effect I've run into is the inability to restart your code from within pdb (using run) while debugging during a nose run.

like image 186
Matt Luongo Avatar answered Sep 21 '22 04:09

Matt Luongo


You can add

import pdb; pdb.set_trace()  

anywhere in your source that you want to stop in the debugger.

Make sure you pass -s to nose so that it does not capture stdout.

like image 27
Ned Batchelder Avatar answered Sep 20 '22 04:09

Ned Batchelder