27

我们需要使用Google Test Framework为wxWidgets应用程序编写单元测试。问题是wxWidgets使用宏IMPLEMENT_APP(MyApp)来初始化并进入应用程序主循环。这个宏创建了几个函数,包括int main()。google 测试框架还为每个测试使用宏定义。

其中一个问题是无法从测试宏中调用 wxWidgets 宏,因为第一个宏会创建函数。因此,我们发现可以将宏替换为以下代码:

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

这是一个很好的替代品,但是 wxEntry() 调用会进入原始应用程序循环。如果我们不调用 wxEntry() ,应用程序的某些部分仍然没有初始化。

问题是如何初始化 wxApp 运行所需的一切,而不实际运行它,以便我们能够对它的部分进行单元测试?

4

6 回答 6

16

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.

于 2009-05-28T12:01:04.293 回答
9
IMPLEMENT_APP_NO_MAIN(MyApp);
IMPLEMENT_WX_THEME_SUPPORT;

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

    return 0;
}
于 2013-10-10T12:35:05.750 回答
5

您想使用以下功能:

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.

于 2008-10-17T16:55:45.857 回答
1

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

 
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif
#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
{
public:
    virtual bool OnInit();
    virtual int OnRun();
};

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{   
    return true;
}

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

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

    return result.FailureCount(); 
}
于 2011-04-15T14:33:36.470 回答
-1

你可以扭转局面:

初始化并启动 wxPython 应用程序,包括主循环,然后在应用程序内运行单元测试。我认为在完成所有初始化工作之后,在主循环入口处调用了一个函数。

于 2008-10-16T18:42:22.753 回答
-1

你试过IMPLEMENT_APP_NO_MAIN宏吗?宏定义上方提供的注释表明它可能会执行您需要的操作。

从 <wxWidgets' 源目录>\include\wx.h:

// Use this macro if you want to define your own main() or WinMain() function
// and call wxEntry() from there.
#define IMPLEMENT_APP_NO_MAIN(appname)                                      \
   wxAppConsole *wxCreateApp()                                             \
    {                                                                       \
        wxAppConsole::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE,         \
                                        "your program");                    \
        return new appname;                                                 \
    }                                                                       \
    wxAppInitializer                                                        \
        wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp);        \
    DECLARE_APP(appname)                                                    \
    appname& wxGetApp() { return *wx_static_cast(appname*, wxApp::GetInstance()); }
于 2008-10-17T00:27:59.267 回答