category: C / C++
DATE : 2006/11/03 (Fri)
DATE : 2006/11/03 (Fri)
(前回の記事)
テスト用アプリケーションの記述
ユニットテストを実行するアプリケーションを記述します。
テスト用のアプリケーションは、Brew Unit Test に付属するサンプルコードを流用すると、簡単に記述できます。
サンプルコードは、「src\platforms\brew」にある brewunit.cpp です。
以下に、 brewunit.cpp の一部を転載します。(インデントなどは、記事に合わせて変更してあります)
#include "stdafx.h"
// include base files for BrewTestUnit library
#include "UnitTestCase.h"
#include "UnitTestResult.h"
#include "UnitTestSuite.h"
// add brew-specific listeners support
#include "stdlistener.h"
#include "filelistener.h"
#include "cppunit.bid" // applet class ID
// add multiplatform examples
#include "../examples/testcase1.h"
#include "../examples/testcase2.h"
static bool CppUnit_HandleEvent(IApplet * pi, AEEEvent eCode,
uint16 wParam, uint32 dwParam);
int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,
IModule * po,void ** ppObj)
{
*ppObj = NULL;
if(ClsId == AEECLSID_CPPUNIT)
{
if(AEEApplet_New(sizeof(AEEApplet), ClsId, pIShell,
po,(IApplet**)ppObj,
(AEEHANDLER)CppUnit_HandleEvent,NULL)
== TRUE)
{
return AEE_SUCCESS;
}
}
return EFAILED;
}
static bool CppUnit_HandleEvent(IApplet * pi, AEEEvent eCode,
uint16 wParam, uint32 dwParam)
{
AEEApplet * pMe = (AEEApplet*)pi;
switch (eCode)
{
case EVT_APP_START:
AECHAR * forDraw;
{
CppUnit::TestResult tr; // accumulate test's statistics
// create and register some listeners
StandardListener stdListener;
FileListener fileListener;
tr.addListener(stdListener);
tr.addListener(fileListener);
// create main test suite with some test cases
// (or test suites) and run its
MyTestCase1 tc1(_T("check account"));
MyTestCase2 tc2(_T("security test"));
CppUnit::TestSuite ts;
ts.addTestCase(tc1);
ts.addTestCase(tc2);
ts.run(tr);
// we can use TestResult in direct way
// (instead of use listeners)
if (tr.failureCount() == 0)
forDraw = L"Tests OK";
else
forDraw = L"Tests FAILED";
}
// Display result string on screen
IDISPLAY_DrawText(pMe->m_pIDisplay, AEE_FONT_BOLD, forDraw, -1,
0, 0, 0, IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
IDISPLAY_Update (pMe->m_pIDisplay);
return true;
case EVT_APP_STOP:
return true;
default:
break;
}
return false;
}
まず次の部分をテスト用アプリケーションのために生成した bid ファイルに変更します。
#include "cppunit.bid" // applet class ID
続いて以下の部分を、作成したテストケースのヘッダファイルを取り込むように変更します。
// add multiplatform examples #include "../examples/testcase1.h" #include "../examples/testcase2.h"
最後に、テストケースを生成してテストスイートに登録します。変更する部分は、次の通りです。
// create main test suite with some test cases
// (or test suites) and run its
MyTestCase1 tc1(_T("check account"));
MyTestCase2 tc2(_T("security test"));
CppUnit::TestSuite ts;
ts.addTestCase(tc1);
ts.addTestCase(tc2);
ts.run(tr);
例えば、「SampleTest」(SampleTest.h)というテストケースを登録する場合は、次のようになります。
#include "SampleTest.h"
// create main test suite with some test cases
// (or test suites) and run its
SampleTest sampleTest;
CppUnit::TestSuite ts;
ts.addTestCase(sampleTest);
ts.run(tr);
テストケースの分だけ、テストケースの取り込み、テストケースの生成、テストスイートに登録という作業を繰り返すことになります。
以上で、ユニットテストを実行するアプリケーションができました。あとは、このアプリケーションをビルドして BREW シミュレータで実行するだけです。
なお、シミュレータの「BREW 出力ウィンドウ」とファイルにテストの結果が出力されます。
(了)
PR
●この記事にコメントする
忍者ブログ [PR]
