看不到正在抛出的异常

看不到正在抛出的异常

本文介绍了Python:看不到正在抛出的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行单元测试,但我意识到会抛出异常.但是,我只是不确定到底会抛出什么.

I am running a unit test and I realize that there is an exception getting thrown. However, I am just not sure what exactly is getting thrown.

from pt_hil.utilities.PT_HIL_Interface_Utils.widgets import PathPicker
import unittest
import wx

class TestUM(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print 'setUpClass called'
        cls.path_picker = PathPicker()
        print 'path_picker has been declared'

    def test_PathPicker(self):
        self.assertRaises(NotImplementedError, wx.UIActionSimulator.MouseClick(self.path_picker.browse))

if __name__ == '__main__':
    unittest.main()

PathPicker类:​​

the PathPicker class:

class PathPicker(Widget):

    def __init__(self, parent=None, name="PathPicker"):
        print 'hi1'
        try:
            Widget.__init__(self, name, parent)
        except Exception as e:
            print 'hello'
            return logging.error(traceback.format_exc())
        print 'hi2'

运行单元测试时得到的输出是:

the output I get when I run the unit test is:

setUpClass called
hi1

Process finished with exit code 1

很明显,Widget.__init__(self, name, parent)出了点问题,但我看不到它是什么.有什么办法可以让我打印出抛出了什么异常或错误?

So clearly, something is going wrong at: Widget.__init__(self, name, parent) but I can't see what it is. Is there any way I can get this to print out what exception or error is getting thrown?

这是与之一起使用的Widget类:

edit: here is the Widget class to go along with it:

class Widget(QWidget):
    def __init__(self, name, parent=None):
        print 'hey2'
        try:
            super(Widget, self).__init__()
        except BaseException as e:
            print 'hello'
            return logging.error(traceback.format_exc())
        print 'hey3'

现在它给了我:

setUpClass called
hi1
hey2

Process finished with exit code 1

推荐答案

我需要在脚本中用class TestUM(unittest.TestCase):

因此上面的脚本应如下所示:

so that script above should look like:

from pt_hil.utilities.PT_HIL_Interface_Utils.widgets import PathPicker
import unittest
import wx

class TestUM(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print 'setUpClass called'
        cls.path_picker = PathPicker()
        print 'path_picker has been declared'

    def test_PathPicker(self):
        self.assertRaises(NotImplementedError, wx.UIActionSimulator.MouseClick(self.path_picker.browse))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    unittest.main()
    sys.exit(app.exec_())

请注意,这不能解决我抛出我需要的异常的问题(因为没有可见的异常).但是它确实解决了问题,脚本将运行.谢谢!

note that this does not solve the problem I had of throwing the exception I needed (since no exception was visible). But it did solve the problem and the script would run. Thanks!

这篇关于Python:看不到正在抛出的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 16:51