【问题标题】:ipdb with python unittest module带有 python unittest 模块的 ipdb
【发布时间】:2015-10-16 21:17:34
【问题描述】:

在使用 python 的 unittest 模块运行测试时,是否有任何便捷的方法可以在异常情况下获取 ipdb 调试器?

使用ipython --pdb my_script.py调试python代码很方便。 但是,当我使用 unittest 模块时,使用

class MyTestCase(unittest.TestCase):
    def runTest(self):
        x = 0
        y = 3/x

unittest 捕获异常并退出。

【问题讨论】:

  • nosetests --pdb <test file> 是我所知道的最接近但不使用 ipdb。我添加import ipdb; ipdb.set_trace() 以强制测试亲自放入 ipdb,但我也希望使用 cmd 行方式来执行此操作。

标签: python unit-testing ipdb


【解决方案1】:

你可以在 ipython 中设置一个 %pdb 标志。如果发生任何异常,它将调用调试器。

【讨论】:

    【解决方案2】:

    我发现先运行测试并查看是否发生任何错误很有帮助。这有助于全面了解错误。例如,是否有多个测试失败,应该首先查看哪一个。

    经过分析,这是我的测试/调试周期方法。 在你的测试中:

    def test_foo_is_bar(self):
        import ipdb
        ipdb.set_trace()
        self.assertEqual('foo', 'bar')
    

    现在运行测试:

    nosetests -s tests/test_example.py
    

    -s 标志将帮助您进入输入模式,而不是从鼻子获取异常。

    旁注:我在 IntelliJ(PyCharm) 设置中设置了粘贴 import ipdb as pdb; pdb.set_trace() 的快捷方式,这样我就可以在我的代码中插入这一行来停止我想要的任何位置。

    【讨论】:

      【解决方案3】:

      nose 现在有一个 ipdb 插件。您可以通过以下方式安装它:

      pip install ipdbplugin
      

      然后测试你的程序,

      nosetests --ipdb <test_file>
      

      【讨论】:

        猜你喜欢
        • 2017-10-12
        • 1970-01-01
        • 2021-12-08
        • 1970-01-01
        • 1970-01-01
        • 2016-04-20
        • 2016-04-23
        • 2014-01-26
        • 2020-12-09
        相关资源
        最近更新 更多