2017-08-03 68 views
3

我正在運行一個單元測試,並且我意識到拋出了一個異常。但是,我只是不確定到底拋出了什麼。Python:看不到拋出的異常

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類:​​

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' 

輸出我得到的,當我運行單元測試是:

setUpClass called 
hi1 

Process finished with exit code 1 

所以很明顯,東西在哪裏出問題了:Widget.__init__(self, name, parent)但我可以看不到它是什麼。有沒有什麼辦法可以打印出異常或錯誤?

編輯:這裏是Widget類與它一起去:

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 

回答

1

我需要添加app = QApplication(sys.argv)sys.exit(app.exec_())腳本與class TestUM(unittest.TestCase):

所以上面的腳本應該是這樣的:

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_()) 

注意,這並沒有解決我拋出我需要的異常的問題(因爲沒有例外可見)。但它確實解決了問題並且腳本會運行。謝謝!

2

正如你可以看到here,在蟒蛇(頂部例外2 .x)是:

BaseException 
+-- SystemExit 
+-- KeyboardInterrupt 
+-- GeneratorExit 
+-- Exception 
     +-- StopIteration 
     +-- StandardError 
     .... 

因此,在您的情況下,通過捕獲異常,您缺少一些其他的exceptio ns(罕見的例外,但可能發生在你的情況):SystemExit,KeyboardInterrupt和GeneratorExit。 嘗試改變你的除了從句譯成:

except BaseException as e: 

這樣,你一定會捕獲所有異常,並檢測你的問題。

編輯:

但是,PyQT可以很有趣。作爲mentionned here

在PyQt的V5.5將導致以 Qt的qFatal()函數的調用未處理的Python異常。默認情況下,這將調用abort(),並且應用程序將終止。請注意,安裝了異常掛鉤的應用程序仍然優先。

因此,一個未被意識到的異常(在C++代碼中出現很多原因,可能會發生錯誤的參數...)可以靜默地停止您的應用程序。 然而,最後一部分聽起來很有用,如果你安裝了一個異常掛鉤,它會在無提示放棄之前被調用。讓我們嘗試添加一個例外鉤:

sys._excepthook = sys.excepthook # always save before overriding 

def application_exception_hook(exctype, value, traceback): 
    # Let's try to write the problem 
    print "Exctype : %s, value : %s traceback : %s"%(exctype, value, traceback) 
    # Call the normal Exception hook after (this will probably abort application) 
    sys._excepthook(exctype, value, traceback) 
    sys.exit(1) 

# Do not forget to our exception hook 
sys.excepthook = application_exception_hook 
+0

我試過了。沒有什麼改變。奇怪的。另外,檢查編輯。 – BUInvent

+1

噢,QtWidget初始化中發生了一些奇怪的事情,並且它決定停止執行程序:'( –

+1

@SlakNation回答編輯了新信息 –