首页 文章

如何在运行pytest测试时随意执行ipdb.set_trace()

提问于
浏览
67

我正在为我的测试套件使用pytest . 虽然在复杂的组件间测试中捕获错误,但我想将 import ipdb; ipdb.set_trace() 放在我的代码中间以允许我调试它 .

但是,由于pytest陷阱sys.stdin / sys.stdout ipdb失败 . 如何在使用pytest进行测试时使用ipdb .

我没有兴趣在失败后跳转到pdb或ipdb,但是在代码中的任何地方放置中断并且能够在发生故障之前在那里调试它 .

3 回答

  • 102

    安装pytest-ipdb插件然后使用

    pytest.set_trace()
    
  • 23

    由于py.test捕获输出而引发错误 .

    你应该使用 -s 选项运行py.test(关闭捕获输出) . 例如:

    py.test -s my_test.py
    
  • 19

    遗憾的是,不再支持pytest-ipdb .

    解决方案是运行 pytest my_test.py --pdb --pdbcls=IPython.terminal.debugger:Pdb

    从help命令:

    pytest -h
      --pdb                 start the interactive Python debugger on errors.
      --pdbcls=modulename:classname
                            start a custom interactive Python debugger on errors.
                            For example:
                            --pdbcls=IPython.terminal.debugger:TerminalPdb
    

    区别在于TerminalPdb似乎抛出错误,但Pdb没有(Ipython docs) .

相关问题