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

Just been through this myself with 2.8.10. The magic is this:

// MyWxApp derives from wxApp
wxApp::SetInstance( new MyWxApp() );
wxEntryStart( argc, argv );
wxTheApp->CallOnInit();

// you can create top level-windows here or in OnInit()
...
// do your testing here

wxTheApp->OnRun();
wxTheApp->OnExit();
wxEntryCleanup();

You can just create a wxApp instance rather than deriving your own class using the technique above.

I'm not sure how you expect to do unit testing of your application without entering the mainloop as many wxWidgets components require the delivery of events to function. The usual approach would be to run unit tests after entering the main loop.


IMPLEMENT_APP_NO_MAIN(MyApp);
IMPLEMENT_WX_THEME_SUPPORT;

int main(int argc, char *argv[])
{
    wxEntryStart( argc, argv );
    wxTheApp->CallOnInit();
    wxTheApp->OnRun();

    return 0;
}

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.