How to unit test wxPython?

I heard about unit testing and wrote tests myself, like tests, but I never used any test frameworks. Now I am writing a wxPython GUI for some of my own data analysis / visualization libraries. I read some of the obvious Google results such as http://wiki.wxpython.org/Unit%20Testing%20with%20wxPython and its link http://pywinauto.openqa.org/ , but I still don't know why to begin.

Does anyone have experience or good links for those who know the theory, but have never used any of the frameworks and do not know how this works with graphical interfaces?

I am on a Windows machine developing a theoretically cross-platform application that uses NumPy , Matplotlib , the Newflow MPlot package and wxPython 2.8.11. Python 2.6 with plans 3.1. I work for a bunch of scientists, so there is no internal policy for testing modules.

+4
source share
3 answers

If you want to test your application, you do not need to focus on GUI testing methods. It is much better to write an application using MVC , MVP, or another meta template like this. Thus, you separate the business logic and the presentation layer.

It’s much more important to cover the business layer with tests, as this is your code. The presentation layer has already been tested by wxWidgets developers. To test the business level, just basic tools such as the standard unittest and possibly nose are enough.

To make sure that the entire application is behaving correctly, you should add some acceptance tests that will test functionality from end to end. They will deal with the graphical interface, but such tests will be few in comparison with the number of unit tests.

If you limit yourself to acceptance tests only, you will get low coverage, a fragile and very slow test code base.

+1
source

In unit test, your application, which does not require many piece objects / stubs, your GUI event handlers should basically delegate to other method calls, passing the values ​​from the Event object as parameters to the delegated method.

Otherwise, you will not be able to test your application without creating fake wx objects.

Take a look at the PyPubSub project for a great module that will help with MVC.

+1
source

In one of my early projects, I am really testing a wxPython application using a GUI layer. Therefore, the tests really spin the wxApp live object, the real windows pop up, and then start messing with the real MainLoop (). Very soon, I realize that this is the wrong way to conduct testing. My tests were very slow and unreliable. A much better way is to separate the GUI material and check only the "model" level of your application. Note that you can indeed create a model for presentation-level logic (a model representing some visual part of your application) and test it. But this model should not include any "real" gui objects (windows, dialogs, widgets).

0
source

Source: https://habr.com/ru/post/1315742/


All Articles