setting breakpoints with nosetests --pdb option

余生颓废 提交于 2021-02-05 12:38:48

问题


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.


回答1:


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.




回答2:


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.




回答3:


If you have ipython, for unlimited awesomeness use:

import ipdb; ipdb.set_trace() 

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




回答4:


If you are using pytest, you can use

import pytest; pytest.set_trace()

See documentation.



来源:https://stackoverflow.com/questions/4950637/setting-breakpoints-with-nosetests-pdb-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!