Custom events in unit tests

瀏覽次數:21 次
跳到第一則未讀訊息

Cédric Boudinet

未讀,
2017年11月7日 上午11:19:512017/11/7
收件者:wxPython-users
Hello

I am using custom event in a wxpython gui but I encounter problems with unit test.
Please find enclosed my example codes.
In my example, when the users clicks on the button, MyWidget posts a custom event. This event is caught by the main frame and the label is set to 'Event Ok'.
This works fine, when the script is launched by the user.

However when launched in the unittest (python setup.py test), the result is as follows:

test0 (testgui.Tests_GUI) ... event send
FAIL

======================================================================
FAIL: test0 (testgui.Tests_GUI)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/boudinec/devel/Allobrogia/trunk/tests_wx/customevent/testgui.py", line 21, in test0
    self.assertEqual(self.frame.text.GetLabel(),"event Ok")
AssertionError: u'' != 'event Ok'

The line test0 (testgui.Tests_GUI) ... event send means that when the EVT_COMMAND_BUTTON_CLICKED is processed, OnButton is called and the custom event posted.
However, the fact that the tests fails means that the custom event is not caught by the main frame.

Is is possible to unittest custom events?

Regards

Cedric
testgui.py
customevent_unittest.py
setup.py

Tim Roberts

未讀,
2017年11月7日 下午2:23:342017/11/7
收件者:wxpytho...@googlegroups.com
Cédric Boudinet wrote:
>
> I am using custom event in a wxpython gui but I encounter problems
> with unit test.
> Please find enclosed my example codes.
> In my example, when the users clicks on the button, MyWidget posts a
> custom event. This event is caught by the main frame and the label is
> set to 'Event Ok'.
> This works fine, when the script is launched by the user.
>
> However when launched in the unittest (python setup.py test), the
> result is as follows:
> ...
>
> The line test0 (testgui.Tests_GUI) ... event send means that when the
> EVT_COMMAND_BUTTON_CLICKED is processed, OnButton is called and the
> custom event posted.
> However, the fact that the tests fails means that the custom event is
> not caught by the main frame.

No, actually, that's not what it means.  The custom event IS being sent,
but you used PostEvent, which puts the message in the message queue, to
be handled by the main loop.  But you never give the app a chance to get
back to the main loop, so the event is still in the queue when you check
the label.  Your "time.sleep(1) call is just blocking the whole UI --
nothing can happen while you are sleeping

Change the "time.sleep(1)" to a "wx.Yield()" (which allows the main loop
to run), and you'll see it is working just fine.

--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Cédric Boudinet

未讀,
2017年11月8日 凌晨12:43:262017/11/8
收件者:wxPython-users
It works! Thanks a lot

Bests
Cedric
回覆所有人
回覆作者
轉寄
0 則新訊息