有没有办法用PyCharm捕获unittest异常? [英] Is there a way to catch unittest exceptions with PyCharm?

查看:415
本文介绍了有没有办法用PyCharm捕获unittest异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python unittest runner处理所有异常。我想使用我的调试器来捕捉他们。



有没有办法让我的unittest runner重新提高测试异常以终止进程?
我想自己处理。



修改:找到一个解决方案。



您可以创建一个unittest.TestSuite并调用 debug()运行您要调试的测试 - 包括使用调试器捕获异常!



可以轻松完成此模式:

  import unittest 

class DebuggableTestCase(unittest.TestCase):
@classmethod
def debugTestCase(cls):
loader = unittest.defaultTestLoader
testSuite = loader.loadTestsFromTestCase(cls)
testSuite.debug()

class MyTestCase(DebuggableTestCase):
def test_function_that_fails自我):
raise异常('test')

如果__name__ =='__main__':
MyTestCase.debugTestCase()


解决方案

我知道这是一个老线程,也许有些这些事情不在问题的问题。但是只是为了后代...



您可以使用py.test作为测试运行器运行您的 unittest 测试。您将获得一些伟大的功能开箱即用。直接到您的问题,有一些很好的方法来调试py.test,包括PyCharm。



具体来调试任何故障,验尸,使用PyCharm - 您要求的 - 您可以安装 pytest-pycharm 插件。

  pip install pytest-pycharm 

(我是在一个virtualenv每个项目中工作,所以这对我来说没有什么不利,我建议你做同样的事情,否则,请注意你正在全局安装,这个插件对于使用这个Python interpeter的所有项目都是活跃的。 / p>

另请参见:




The python unittest runner handles all exceptions. I would like to catch them with my debugger.

Is there a way to make my unittest runner re-raise tests exceptions to terminate the process? I want to handle them myself.

Edit: Found a solution.

You can create a unittest.TestSuite and call debug() to run the tests you want to debug - including catching the exceptions with your debugger!

It can be easily done with this pattern:

import unittest

class DebuggableTestCase(unittest.TestCase):
    @classmethod
    def debugTestCase(cls):
        loader = unittest.defaultTestLoader
        testSuite = loader.loadTestsFromTestCase(cls)
        testSuite.debug()

class MyTestCase(DebuggableTestCase):
    def test_function_that_fails(self):
        raise Exception('test')

if __name__ == '__main__':
    MyTestCase.debugTestCase()

解决方案

I know this is an old thread, and perhaps some of these things weren't around when the question was asked. But just for posterity...

You can run your unittest tests with py.test as a test runner. You'll get some great features right out of the box. Directly to the point of your question, there are a few good ways to debug with py.test, including with PyCharm.

Specifically to debug on any failure, post-mortem, with PyCharm - what you are asking for - you can install the pytest-pycharm plugin.

pip install pytest-pycharm

(I am working in a virtualenv per-project so this has no downside for me. I recommend you do the same. Otherwise, be aware that you're installing it globally, and this plugin will be active for all projects using this Python interpeter.)

See also:

这篇关于有没有办法用PyCharm捕获unittest异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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