91

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.

gerrit
  • 24,025
  • 17
  • 97
  • 170
Devin
  • 2,113
  • 2
  • 22
  • 28

4 Answers4

148

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.

Matt Luongo
  • 14,371
  • 6
  • 53
  • 64
  • Is there any documentation of this? How to move out of the loop? – A.J. Jan 04 '16 at 09:42
  • You mean how to navigate once you've got `pdb` running? I'm sure there are docs, but off the top of my head- `s` "steps into" a function call, `n` goes to the "next" statement, `u` moves "up" the stack, and `d` moves "down". You can use `b` to set breakpoints and `c` to "continue" and quit the stepping debugger. Hope that helps! – Matt Luongo Jan 08 '16 at 23:40
  • 4
    This should be the accepted answer. It provides all the regular functionality of the original pdb.set_trace() command. – tbm May 16 '16 at 15:27
  • 1
    Any possibility for an ipython/ipdb version of this? – gerrit May 15 '19 at 15:12
125

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.

Streeter
  • 556
  • 4
  • 22
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • I was just about to add this to my question. This isn't an acceptable solution either. When using this with nosetests, it's apparent this is literally unusable as access to the pdb console is screened, and no pdb commands can be executed. – Devin Feb 09 '11 at 21:32
  • 27
    Add the -s flag to nosetests to prevent it from capturing stdout, and you'll be able to use pdb just fine. – Ned Batchelder Feb 09 '11 at 23:47
  • 1
    Does not work. Use this: `from nose.tools import set_trace; set_trace()` – anilbey Apr 23 '20 at 19:01
5

If you have ipython, for unlimited awesomeness use:

import ipdb; ipdb.set_trace() 

*unlimited awesomeness: just like ipython - auto-completion, coloring etc.

Shubham Chaudhary
  • 47,722
  • 9
  • 78
  • 80
1

If you are using pytest, you can use

import pytest; pytest.set_trace()

See documentation.

gerrit
  • 24,025
  • 17
  • 97
  • 170