CXX=g++LIBS=-lcppunit
SOURCES=someTest.cc some.ccTARGETS=mainTestall : g++ -o mainTest mainTest.cc $(SOURCES) $(LIBS)clean : rm $(TARGET)
CPPUnitを使う上で必要なファイル
- mainTest.cc: メインのテストコードここはテンプレで良さそう
- someTest.cc: 自分のテストコードを書く場所
- some.cc: テスト対象となるコードやクラス
mainTest.cc
#include <cppunit/TestResult.h>#include <cppunit/TestResultCollector.h>#include <cppunit/BriefTestProgressListener.h>#include <cppunit/TestRunner.h>#include <cppunit/extensions/TestFactoryRegistry.h>#include <cppunit/CompilerOutputter.h>using namespace CPPUNIT_NS;int main(int argc, char **argv) { TestResult controller; TestResultCollector result; controller.addListener(&result); BriefTestProgressListener progress; controller.addListener(&progress); TestRunner runner; runner.addTest(TestFactoryRegistry::getRegistry().makeTest()); runner.run(controller); CompilerOutputter outputter(&result, stdCOut()); outputter.write(); return result.wasSuccessful() ? 0 : 1;}
これはこのままで良さそう
someTest.cc
#include "some.cc"#include <cppunit/extensions/HelperMacros.h>using namespace CPPUNIT_NS;class someTest : public TestFixture { CPPUNIT_TEST_SUITE(someTest); CPPUNIT_TEST(test1);
CPPUNIT_TEST(test2); CPPUNIT_TEST_SUITE_END(); void test1(){ CPPUNIT_ASSERT( some(true) == false );
}
void test2(){
CPPUNIT_ASSERT( some(false) == true );
}};CPPUNIT_TEST_SUITE_REGISTRATION(reverseTest);
someがbool値を入れ替えるかどうかのテスト
ソースファイルをインクルードしているけれど、他人に見せるようなものでないならこの方がコンパイルが楽。
some.cc
bool some(bool tf){
return !tf;
}
これで完了。