【问题标题】:How to use %debug to investigate Python unittest failure?如何使用 %debug 调查 Python 单元测试失败?
【发布时间】:2017-05-08 19:18:51
【问题描述】:

如何在 IPython 中运行单个 Python unittest 并使用 %debug 调查断言失败的位置?

具体来说,我想要这个魔法……

$ ipython
In [1]: def broken():
   ...:     DOESNOTEXIST
   ...:    

In [2]: broken()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-38dbcef999e4> in <module>()
----> 1 broken()

<ipython-input-1-42074db4a06b> in broken()
      1 def broken():
----> 2     DOESNOTEXIST
      3 

NameError: name 'DOESNOTEXIST' is not defined

In [3]: %debug
> <ipython-input-1-42074db4a06b>(2)broken()
      1 def broken():
----> 2     DOESNOTEXIST
      3 

ipdb> # Rock on

...在启动单元测试时可用...

In [4]: %run -m unittest some/path/to/brokenness.py

...如果失败,运行 %debug 会将我带到未通过某些测试用例的 raise。

似乎unittest 模块吞噬了异常(正如人们所期望的那样)并且--failfast 标志不会将它们咳出来。我知道我可以实例化测试对象,然后手动调用setUp() 等。但这似乎有点愚蠢和痛苦。我不想设置断点。

【问题讨论】:

  • 我也很乐意采用一个解决方案,只有在失败时我才能进入 IPython。
  • 其实更开心。简单地从 CLI 运行并在触发的断言上获得一个 IPython 友好的调试器会很好。
  • 你可以使用nose在ipython中运行测试。

标签: python unit-testing ipython


【解决方案1】:

使用测试运行器

我建议您使用 pytest-ipdb 插件安装pytest。当您的测试中发生异常时,您应该能够使用 --ipdb 标志以类似的方式运行它以访问 ipdb 调试器。

pip install git+git://github.com/mverteuil/pytest-ipdb.git

然后

In [1]: %run -m pytest --ipdb some/path/to/brokenness.py

或从命令行:

py.test --ipdb some/path/to/brokenness.py

另外,如果你因为某种原因不喜欢 pytest,同样的结果可以用鼻子和nose-ipdb 插件存档。

【讨论】:

    【解决方案2】:

    看看正在运行的nose。这可以通过命令行在 ipython 或 jupyter notebook 中单独运行。

    > pip install nose
    

    在包含测试文件的包目录中,运行:

    > nosetests --pdb
    

    这将自动在所有当前目录和子目录中查找前缀为 test_ 的测试模块。或者,您可以在单独的文件上运行它:

    > nosetests test_filename.py --pdb
    

    还有一个plugin 可以使用ipdb。

    nose 对于在 ipython/jupyter 会话中运行测试特别有用。因此,您可以选择在会话中运行测试,而无需创建单独的文件:

    import nose.tools as nt
    
    def test():
        # Some test code
        return 1
    
    nt.assert_equal(test(), 2)
    

    输出

    ---------------------------------------------------------------------------
    AssertionError                            Traceback (most recent call last)
    <ipython-input-16-edbfc78f9c45> in <module>()
          5     return 1
          6 
    ----> 7 nt.assert_equal(test(), 2)
    
    C:\Anaconda3\envs\betalab\lib\unittest\case.py in assertEqual(self, first, second, msg)
        818         """
        819         assertion_func = self._getAssertEqualityFunc(first, second)
    --> 820         assertion_func(first, second, msg=msg)
        821 
        822     def assertNotEqual(self, first, second, msg=None):
    
    C:\Anaconda3\envs\betalab\lib\unittest\case.py in _baseAssertEqual(self, first, second, msg)
        811             standardMsg = '%s != %s' % _common_shorten_repr(first, second)
        812             msg = self._formatMessage(msg, standardMsg)
    --> 813             raise self.failureException(msg)
        814 
        815     def assertEqual(self, first, second, msg=None):
    
    AssertionError: 1 != 2
    

    【讨论】:

    • 来自nose 文档:“Nose 过去几年一直处于维护模式,如果没有新的人员/团队来接管维护,它可能会停止。新项目应该考虑使用 Nose2,py.test ,或者只是简单的 unittest/unittest2。”
    • 它适用于单元测试。如果您愿意,可以尝试nose2。 pytest 会是最好的,但它还不支持会话测试。
    猜你喜欢
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 2019-02-06
    • 2012-06-01
    • 1970-01-01
    • 2020-04-09
    相关资源
    最近更新 更多