以某种方式使用 py.test 时,我可以使用 python 调试器进行调试吗? [英] Can I debug with python debugger when using py.test somehow?

查看:32
本文介绍了以某种方式使用 py.test 时,我可以使用 python 调试器进行调试吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 py.test 对我的 python 程序进行单元测试.我希望使用 python 调试器以正常方式调试我的测试代码(我的意思是代码中的 pdb.set_trace() ),但我无法让它工作.

I am using py.test for unit testing my python program. I wish to debug my test code with the python debugger the normal way (by which I mean pdb.set_trace() in the code) but I can't make it work.

将 pdb.set_trace() 放在代码中不起作用(引发 IOError:在捕获输出时从 stdin 读取).我也尝试使用选项 --pdb 运行 py.test 但如果我想探索在我的断言之前发生的事情,这似乎并没有奏效.当断言失败时它会中断,从该行开始就意味着终止程序.

Putting pdb.set_trace() in the code doesn't work (raises IOError: reading from stdin while output is captured). I have also tried running py.test with the option --pdb but that doesn't seem to do the trick if I want to explore what happens before my assertion. It breaks when an assertion fails, and moving on from that line means terminating the program.

有没有人知道调试的方法,或者调试和 py.test 不应该在一起?

Does anyone know a way to get debugging, or is debugging and py.test just not meant to be together?

推荐答案

其实很简单:将 assert 0 放在您想要在代码中开始调试并运行测试的位置:

it's real simple: put an assert 0 where you want to start debugging in your code and run your tests with:

py.test --pdb 

完成:)

或者,如果您使用 pytest-2.0.1 或更高版本,还有 pytest.set_trace() 帮助程序,您可以将其放在测试代码的任何位置.以下是文档.在将您发送到 pdb 调试器命令行之前,它会注意在内部禁用捕获.

Alternatively, if you are using pytest-2.0.1 or above, there also is the pytest.set_trace() helper which you can put anywhere in your test code. Here are the docs. It will take care to internally disable capturing before sending you to the pdb debugger command-line.

这篇关于以某种方式使用 py.test 时,我可以使用 python 调试器进行调试吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆