- Object Mentor Downloads にある CppUnitLite から CppUnitLite.zip をダウンロードする
- ダウンロードしたファイルを展開すると StackMain.cpp, StackTest.cpp, Stack.h とCppUnitLite フォルダがある
- Makefile を書く
CC = g++ STACK_SRC = \ StackMain.cpp \ StackTest.cpp CPPUNITLITE_SRC = \ CppUnitLite/Test.cpp \ CppUnitLite/TestResult.cpp \ CppUnitLite/TestRegistry.cpp \ CppUnitLite/Failure.cpp \ CppUnitLite/SimpleString.cpp default: StackMain.exe StackMain.exe: $(CC) -o $@ $(STACK_SRC) $(CPPUNITLITE_SRC)
- コンパイル & 実行
> make g++ -o StackMain.exe StackMain.cpp StackTest.cpp CppUnitLite/Test.cpp CppUnitLite/TestResult.cpp CppUnitLite/TestRegistry.cpp CppUnitLite/Failure.cpp CppUnitLite/SimpleString.cpp > StackMain.exe There were no test failures
自分でクラスを作ってテストを書いてみる
Example 4. Counter.h
#ifndef __COUNTER_H__ #define __COUNTER_H__ class Counter { private: int count; public: Counter(); ~Counter(); int get() const; void increment(); void clear(); }; #endif /* __COUNTER_H__ */Example 5. Counter.cpp
#include "Counter.h" // Constractor Counter::Counter() { count = 0; } // Destructor Counter::~Counter() { } // 値を取得 int Counter::get(void) const { return count; } void Counter::increment(void) { count++; } void Counter::clear(void) { count = 0; }Example 6. CounterTest.cpp
#include "CppUnitLite\TestHarness.h" #include "Counter.h" #include <string> SimpleString StringFrom(const std::string& value) { return SimpleString(value.c_str()); } TEST(Counter, creation) { Counter c; LONGS_EQUAL(0, c.get()); // 初期値は 0 c.increment(); LONGS_EQUAL(1, c.get()); // +1 されている c.clear(); LONGS_EQUAL(0, c.get()); // clear したら 0 になっている LONGS_EQUAL(-1, c.get()); // 失敗例 } int main(void) { TestResult tr; TestRegistry::runAllTests(tr); return 0; }Example 7. Makefile
CC = g++ CPPUNITLITE_SRC = \ CppUnitLite/Test.cpp \ CppUnitLite/TestResult.cpp \ CppUnitLite/TestRegistry.cpp \ CppUnitLite/Failure.cpp \ CppUnitLite/SimpleString.cpp COUNTER_SRC = \ Counter.cpp \ CounterTest.cpp CounterTest.exe: $(CC) -o $@ $(COUNTER_SRC) $(CPPUNITLITE_SRC) clean: rm -rf *.exeコンパイル & 実行
> make CounterTest.exe g++ -o CounterTest.exe Counter.cpp CounterTest.cpp CppUnitLite/Test.cpp CppUnitLite/TestResult.cpp CppUnitLite/TestRegistry.cpp CppUnitLite/Failure.cpp CppUnitLite/SimpleString.cpp > CounterTest.exe Failure: "expected -1 but was: 0" line 20 in CounterTest.cpp There were 1 failures
0 件のコメント:
コメントを投稿