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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4950637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 18:18:49  来源:igfitidea点击:

setting breakpoints with nosetests --pdb option

pythontestingnosepdb

提问by Devin

nosetests --pdblet'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.

使用导入 pdb;pdb.set_trace() 看起来是个好主意,但是鼻子测试阻止了我对 pdb 控制台的访问。

采纳答案by Ned Batchelder

You can add

你可以加

import pdb; pdb.set_trace() 

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

您想要在调试器中停止的源代码中的任何位置。

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

确保你传递-s到鼻子,这样它就不会捕捉到stdout

回答by Matt Luongo

Even better than remembering to use -sis to use the set_tracevariant that comes with Nose. Add

比记住使用更好的-s是使用set_traceNose 附带的变体。添加

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.

任何你想进入调试器的地方。标准输入/输出重定向将为您处理。我遇到的唯一奇怪的副作用是run在鼻子运行期间调试时无法从 pdb 中重新启动代码(使用)。

回答by Shubham Chaudhary

If you have ipython, for unlimited awesomeness use:

如果你有ipython,可以无限使用:

import ipdb; ipdb.set_trace() 

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

*无限精彩:就像 ipython - 自动完成、着色等。

回答by gerrit

If you are using pytest, you can use

如果您使用的是pytest,则可以使用

import pytest; pytest.set_trace()

See documentation.

请参阅文档