使用nosetests设置断点--pdb选项

时间:2021-04-03 09:22:15

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.

Nosetests --pdb让我停止错误或失败,但这对我的需求来说太迟了。在执行期间单步执行代码可帮助我调试问题所在。

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

但是,nosetests是有用的,因为它们允许依赖相对导入的测试(即在包中进行测试)。

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.

这种解决方案是不够的。 Nosetests干扰pdb输出,我的键盘控制(例如箭头键)被破坏。

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

使用import pdb; pdb.set_trace()似乎是一个好主意,但是nosetests阻止了我对pdb控制台的访问。

3 个解决方案

#1


95  

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.

确保将-s传递给nose,以便它不会捕获stdout。

#2


124  

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

甚至比记住使用-s更好的是使用Nose附带的set_trace变体。加

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.

无论你想进入调试器的哪个地方。 stdin / out重定向将为您完成。我遇到的唯一奇怪的副作用是在鼻子运行期间调试时无法从pdb(使用run)重启代码。

#3


3  

If you have ipython, for unlimited awesomeness use:

如果你有ipython,无限制的使用:

import ipdb; ipdb.set_trace() 

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

*无限可怕:就像ipython一样 - 自动完成,着色等。

#1


95  

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.

确保将-s传递给nose,以便它不会捕获stdout。

#2


124  

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

甚至比记住使用-s更好的是使用Nose附带的set_trace变体。加

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.

无论你想进入调试器的哪个地方。 stdin / out重定向将为您完成。我遇到的唯一奇怪的副作用是在鼻子运行期间调试时无法从pdb(使用run)重启代码。

#3


3  

If you have ipython, for unlimited awesomeness use:

如果你有ipython,无限制的使用:

import ipdb; ipdb.set_trace() 

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

*无限可怕:就像ipython一样 - 自动完成,着色等。