[ACCEPTED]-wxWidgets: How to initialize wxApp without using macros and without entering the main application loop?-googletest

Accepted answer
Score: 17

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

// MyWxApp derives from wxApp
wxApp::SetInstance( new MyWxApp() );
wxEntryStart( argc, argv );

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


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

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

Score: 9

int main(int argc, char *argv[])
    wxEntryStart( argc, argv );

    return 0;


Score: 5

You want to use the function:

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

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

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

You'll 1 need to use

void wxEntryCleanup()

when you're done.

Score: 1

It looks like doing the tests in the wxApp::OnRun() function 2 can perhaps work. Here's code that tests 1 a dialog's title with cppUnitLite2.

#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#include  "wx/app.h"  // use square braces for wx includes: I made quotes to overcome issue in HTML render
#include  "wx/Frame.h"
#include "../CppUnitLite2\src/CppUnitLite2.h"
#include "../CppUnitLite2\src/TestResultStdErr.h" 
#include "../theAppToBeTested/MyDialog.h"
 TEST (MyFirstTest)
    // The "Hello World" of the test system
    int a = 102;
    CHECK_EQUAL (102, a);

 TEST (MySecondTest)
    MyDialog dlg(NULL);   // instantiate a class derived from wxDialog
    CHECK_EQUAL ("HELLO", dlg.GetTitle()); // Expecting this to fail: title should be "MY DIALOG" 

class MyApp: public wxApp
    virtual bool OnInit();
    virtual int OnRun();


bool MyApp::OnInit()
    return true;

int MyApp::OnRun()
    fprintf(stderr, "====================== Running App Unit Tests =============================\n");
    if ( !wxApp::OnInit() )
        return false;

    TestResultStdErr result;
    fprintf(stderr, "====================== Testing end: %ld errors =============================\n", result.FailureCount() );

    return result.FailureCount(); 

More Related questions