Custom events in unit tests

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Custom events in unit tests

Cédric Boudinet
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

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

testgui.py (646 bytes) Download Attachment
customevent_unittest.py (1K) Download Attachment
setup.py (168 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Custom events in unit tests

Tim Roberts
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, [hidden email]
Providenza & Boekelheide, Inc.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Custom events in unit tests

Cédric Boudinet
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, <a href="javascript:" target="_blank" gdf-obfuscated-mailto="YoeODhyaCwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">ti...@...
Providenza & Boekelheide, Inc.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.