Custom events in unit tests

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

testgui.py (624 Bytes)

customevent_unittest.py (1.27 KB)

setup.py (160 Bytes)

···

======================================================================
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

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, timr@probo.com
Providenza & Boekelheide, Inc.

It works! Thanks a lot

Bests
Cedric

···

On Tuesday, 7 November 2017 20:23:34 UTC+1, Tim Roberts wrote:

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.