2006-08-24(木) 曇ときどき雨 安静時心拍数 64 [長年日記]
_ HP-UXにAutomakeが無くて嵌る
LinuxでAutomakeを使ってCppUnitを組み込んだスケルトンをあっさりと完成させて意気揚揚としていたのですが、HP-UXの環境でいざ
% ./configure % make
してみるとaclocalが無いとほざいてきました。HP-UXに自由にソフトをインストールできる権限が無いので、Automakeを野良インストールするしかないのですが、他の人がmakeするときにいろいろ不便なのでAutoconfだけで頑張るしかないのかな。Makefile.inを手で書くのが面倒です。
Linuxの環境
$ uname -a Linux hoge 2.6.9-5.ELsmp #1 SMP Wed Jan 5 19:30:39 EST 2005 i686 i686 i386 GNU/Linux $ autoconf --version autoconf (GNU Autoconf) 2.59 Written by David J. MacKenzie and Akim Demaille. Copyright (C) 2003 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ automake --version automake (GNU automake) 1.9.2 Written by Tom Tromey. Copyright 2004 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
HP-UXの環境
% uname -a HP-UX fuga B.10.20 A 9000/871 2013667818 2-ユーザ・ライセンス % autoconf --version Autoconf version 2.9
_ CppUnit実践
まずはCppUnitのmain関数ですが、これはcppunit-1.12.0/examples/simple/Main.cppを拾ってくれば良いです。
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
int
main( int argc, char* argv[] )
{
// Create the event manager and test controller
CPPUNIT_NS::TestResult controller;
// Add a listener that colllects test result
CPPUNIT_NS::TestResultCollector result;
controller.addListener( &result );
// Add a listener that print dots as test run.
CPPUNIT_NS::BriefTestProgressListener progress;
controller.addListener( &progress );
// Add the top suite to the test runner
CPPUNIT_NS::TestRunner runner;
runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
runner.run( controller );
// Print test in a compiler compatible format.
CPPUNIT_NS::CompilerOutputter outputter( &result, CPPUNIT_NS::stdCOut() );
outputter.write();
return result.wasSuccessful() ? EXIT_SUCCESS : EXIT_FAILURE;
}
次に、これからHogeクラスを作っていくという前提でHogeクラスに対するテストコードを書いていきます。ヘッダファイルHogeTest.hを作ります。
#ifndef HogeTest_h
#define HogeTest_h
#include <cppunit/TestCase.h>
#include <cppunit/extensions/HelperMacros.h>
class HogeTest : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE(HogeTest);
CPPUNIT_TEST(testgetVal);
CPPUNIT_TEST(testsetVal);
CPPUNIT_TEST_SUITE_END();
public:
SupplyManagementTest(void)
{
}
virtual ~SupplyManagementTest(void)
{
}
void setUp()
{
}
void tearDown()
{
}
void testgetVal();
void testsetVal();
};
#endif
そして実装のHogeTest.cpp
#include "Hoge.h"
#include "HogeTest.h"
CPPUNIT_TEST_SUITE_REGISTRATION(HogeTest);
void HogeTest::testgetVal()
{
Hoge hoge;
CPPUNIT_ASSERT_EQUAL(1, hoge.getVal());
}
void HogeTest::testsetVal()
{
// まかせた
}
HogeTest.cppを適切に書いたらテストを実施しつつHoge.cppを書きます。
凝ったmain関数はCppUnit のメイン関数を作成するを参照。
[ツッコミを入れる]