#include "CppUTest/CommandLineTestRunner.h" int main(int argc, char** argv) { return CommandLineTestRunner::RunAllTests(argc, argv); }
/cygdrive/c/xxxx(ここは任意)\CppUTest\include (ここで、C:と指定してはいけないことに注意! cygwinをeclipseで使おうとするときは、/cygdrive/c/で置き換えなければダメ)&br; 追記:/cygdrive/cだと、eclipseで includeファイルを認識してくれない。&br; その場合は、/cygdrive/c/とC:\を両方登録する。
CppUTest
/cygdrive/c/xxxx(ここは任意)\CppUTest\lib (ここで、C:と指定してはいけないことに注意! cygwinをeclipseで使おうとするときは、/cygdrive/c/で置き換えなければダメ)
OK (0 tests, 0 ran, 0 checks, 0 ignored, 0 filtered out, 2 ms)
#include "CppUTest/TestHarness.h" #include "Die.h" TEST_GROUP(Die){ }; TEST(Die, InitialValueAfterCreationIsValid) { Die d; CHECK(d.faceValue() >= 1); CHECK(d.faceValue() <= 6); }
#include "Die.h" Die::Die() { } Die::~Die() { } int Die::faceValue() const { return 1; }
#include "Die.h" Die::Die() { } Die::~Die() { } int Die::faceValue() const { return 0; }
../DieTest.cpp:10: error: Failure in TEST(Die, InitialValueAfterCreationIsValid) CHECK(d.faceValue() >= 1) failed . Errors (1 failures, 1 tests, 1 ran, 1 checks, 0 ignored, 0 filtered out, 3 ms)
#include "Die.h" Die::Die() { } Die::~Die() { } int Die::faceValue() const { return 1; /**N ここを0から1に修正 */ }
. OK (1 tests, 1 ran, 2 checks, 0 ignored, 0 filtered out, 1 ms)
#ifndef DIE_H_ #define DIE_H_ int faceValue(void); #endif /* DIE_H_ */
#include "Die.h" int faceValue(void) { return 1; }
#include "CppUTest/TestHarness.h" extern "C" { #include "Die.h" } TEST_GROUP(Die){ }; TEST(Die, InitialValueAfterCreationIsValid) { CHECK(faceValue() >= 1); CHECK(faceValue() <= 6); }
. OK (1 tests, 1 ran, 2 checks, 0 ignored, 0 filtered out, 2 ms)
C言語でも、CPPUTestは使えます。 キモはテスト関数を.cppでつくり、ヘッダファイルをextern"C"{}で囲むこと
extern "C" { #include "xxx.h" }
> *.exe -ojunit
ここまでは基本。これから応用。
-ojunit
これであなたも 「レッド」→ 「グリーン」→ 「リファクタリング」
--+--include | +--lib | +--objs | +--src | +--tests | | | +---testsrc | | | +---RunAlltests.cpp | +--mocks