2016-11-08 5 views
1

OKボタンをクリックするとTypeError例外が発生するwxpythonダイアログがあります。私はunittestで例外の発生をテストしたいが、テストは期待通りに動作しない。出力は、例外が発生したことを示します。とにかくunittestのテストが失敗したことを通知します。ここではunittest:wxpythonのイベントメソッドは例外を発生させますが、assertRaisesはそれを検出しません。

"C:\Program Files (x86)\Python\python.exe" test.py 
Traceback (most recent call last): 
    File "test.py", line 22, in on_ok 
    raise TypeError('TypeError raised') 
TypeError: TypeError raised 
F 
====================================================================== 
FAIL: test_should_raise (__main__.CDlgTest) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "test.py", line 34, in test_should_raise 
    self._dut.m_button_ok.GetEventHandler().ProcessEvent(event) 
AssertionError: TypeError not raised 

---------------------------------------------------------------------- 
Ran 1 test in 0.005s 

FAILED (failures=1) 

は、私のコードの減少したサンプルです:

import unittest 
import wx 

class CDlgBase (wx.Dialog): 
    """The UI""" 
    def __init__(self, parent): 
     wx.Dialog.__init__ (self, parent) 
     bSizerTest = wx.BoxSizer(wx.VERTICAL) 
     self.m_button_ok = wx.Button(self, wx.ID_ANY) 
     bSizerTest.Add(self.m_button_ok, 0) 
     self.SetSizer(bSizerTest) 
     # Connect Events 
     self.m_button_ok.Bind(wx.EVT_BUTTON, self.on_ok) 
    def on_ok(self, event): 
     event.Skip() 

class CDlg(CDlgBase) : 
    """The dialog""" 
    def __init__(self, parent): 
     super(CDlg, self).__init__(parent) 
    def on_ok(self, event): 
     # The exception should be verified in the test `test_should_raise()`. 
     raise TypeError('TypeError raised') 

class CDlgTest(unittest.TestCase) : 
    """The test class""" 
    def setUp(self): 
     self._dut = CDlg(None) 
    def test_should_raise(self): 
     """The test to verify raising the TypeError exception in the event 
     method `on_ok()`. this is the test method wich works not as expected.""" 
     event = wx.CommandEvent(wx.EVT_BUTTON.evtType[0]) 
     event.SetEventObject(self._dut.m_button_ok) 
     with self.assertRaises(TypeError) : 
      """Simulate an "OK" click. `on_ok()` will be executed 
      and raises the TypeError exception.""" 
      self._dut.m_button_ok.GetEventHandler().ProcessEvent(event) 

if __name__ == '__main__': 
    app = wx.App() 
    tests = [ unittest.TestLoader().loadTestsFromTestCase(CDlgTest) ] 
    unittest.TextTestRunner(verbosity=2, failfast=True).run(unittest.TestSuite(tests)) 

誰かが私が間違っていた何かを見つけるために私を助けてくださいことはできますか?

+0

私は自分自身がこの問題に対する解決策を探しています。私の理解では、例外は別のスレッドで発生し、wxフレームワークによって捕捉されます。おそらくそのトレースは 'stdout'にダンプされます。 –

+0

別のPythonフォーラムから私は以下の答えを受け取りました。これはGUIテストの一般的な問題です。イベントハンドラでスローされた例外は、完全なプログラムに影響を与えるべきではありません。スケジューラがそれらをキャッチしてテストルーチンに転送しない間に、これが理由です。イベントハンドラは、GUIなしで個別にテストする必要のある薄いレイヤでなければなりません。 – Humbalan

答えて

2

参照:https://wiki.wxpython.org/CppAndPythonSandwich

例外はC++層ものの、コールスタックに渡されません。コントロールがPythonからC++に戻ったとき、捕捉されなかったPython例外があるかどうかをチェックし、そうであればエラーを出力してクリアします。

ユニットテストでこれを処理する1つの方法は、イベントハンドラで例外を捕捉し、フラグを設定することです。テストコードに戻り、そのフラグが設定されているかどうかを確認できます。

+0

私はそれが私のために働くあなたの助言を試みた。どうもありがとう。とにかく、例外なく動作するようにバリデーターを再設定することを考えています。 – Humbalan

関連する問題