This test is not a complete test of the system, of course. We could also test that the TextField instances in the frame were updated with the values after the model was updated. That test would be reasonably straightforward. Another test you might want to run would be to automatically generate the button-click event itself, and ensure that the proper handler is called. That test is a little less straightforward. Listing 5.14 shows an example:
Listing 5.14 A sample test by generating a user event def testEvent(self):
panel = self.frame.GetChildren() for each in panel.GetChildren():
if each.GetLabel() == "Wilmafy": wilma = each break event = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, wilma.GetId()) wilma.GetEventHandler().ProcessEvent(event) self.assertEqual("Wilma", self.frame.model.first) self.assertEqual("Flintstone", self.frame.model.last)
The first few lines of this example find the appropriate button (in this case, the "Wilmafy" button). Since we did not explicitly store the buttons as Python instance variables, we just need to walk through the panel's children list until we find the right button. (You could also do this as a Python list comprehension if you wanted). The next two lines create the wx.CommandEvent to be sent from the button. The single parameter to the creator is wx.wxEVT_COMMAND_BUTTON_ CLICKED, a constant for the actual integer event type that is bound to the EVT_ BUTTON binder object. (You can find the integer constants in the wxPython source file wx.py). After that, we set the ID of the event to the ID of the Wilmafy button. At this point, the event has all the relevant features of the actual event as it would be created by wxPython. So, we call ProcessEvent() to send it into the system. If the code works as planned, then the model first and last names will be changed to "Wilma" and "Flintstone."
By generating events, you can test the responsiveness of your system from beginning to end. In theory, you could generate a mouse-down and mouse-up event within your button to ensure that the button-click event is created as a response. In practice, this won't work with native widgets because the low level wx.Events aren't translated back into native system events and sent to the native widget. However, a similar process could be useful when testing custom widgets (such as the two-button control in chapter 3). This kind of unit testing can give you confidence in the responsiveness of your application.
Was this article helpful?