看起来在wxApp :: OnRun()函数中进行测试可能有效。以下是使用cppUnitLite2测试对话框标题的代码。
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include "wx/app.h" // wx 包含文件使用方括号,我在引用时使用了双引号以避免 HTML 渲染问题
#include "wx/Frame.h"
#include "../CppUnitLite2\src/CppUnitLite2.h"
#include "../CppUnitLite2\src/TestResultStdErr.h"
#include "../theAppToBeTested/MyDialog.h"
TEST(MyFirstTest)
{
// 测试系统的“Hello World”
int a = 102;
CHECK_EQUAL(102, a);
}
TEST(MySecondTest)
{
MyDialog dlg(NULL); // 实例化一个派生自 wxDialog 的类
CHECK_EQUAL("HELLO", dlg.GetTitle()); // 预期此测试失败:标题应为“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();
}