Search code examples
c++unit-testingwxwidgetsgoogletest

wxWidgets: How to initialize wxApp without using macros and without entering the main application loop?


We need to write unit tests for a wxWidgets application using Google Test Framework. The problem is that wxWidgets uses the macro IMPLEMENT_APP(MyApp) to initialize and enter the application main loop. This macro creates several functions including int main(). The google test framework also uses macro definitions for each test.

One of the problems is that it is not possible to call the wxWidgets macro from within the test macro, because the first one creates functions.. So, we found that we could replace the macro with the following code:

wxApp* pApp = new MyApp(); 
wxApp::SetInstance(pApp);
wxEntry(argc, argv);

That's a good replacement, but wxEntry() call enters the original application loop. If we don't call wxEntry() there are still some parts of the application not initialized.

The question is how to initialize everything required for a wxApp to run, without actually running it, so we are able to unit test portions of it?


Solution

  • You want to use the function:

    bool wxEntryStart(int& argc, wxChar **argv)
    

    instead of wxEntry. It doesn't call your app's OnInit() or run the main loop.

    You can call wxTheApp->CallOnInit() to invoke OnInit() when needed in your tests.

    You'll need to use

    void wxEntryCleanup()
    

    when you're done.