marunomaruno-memo

marunomaruno-memo

CUnit for Mr.Ando.

2006年11月05日 | C / C++
■CUnit

CUnit は、 C / C++ プログラムの単体テストを行うツールです。

■CUnit

CUnit for Mr.Ando.
http://park.ruru.ne.jp/ando/work/CUnitForAndo/html/index_ja.html

■インストール

CUnitForAndo.tar.gz を展開します。

./CUnitForAndo/CUnitForAndo -- 本体.
./CUnitForAndo/easySample -- 簡単なサンプル.
./CUnitForAndo/Sample1/test
./CUnitForAndo/Sample1/test1
./CUnitForAndo/Sample1/test2
./CUnitForAndo/Sample1/real -- サンプル1.
./CUnitForAndo/Sample1/test
./CUnitForAndo/Sample2/real -- サンプル2.
./CUnitForAndo/Sample2/test
./CUnitForAndo/Sample3/real -- サンプル3.
./CUnitForAndo/Sample3/test
./Makefile -- 圧縮とテスト用.
./README.txt -- ライセンス.
./CUnitForAndo/html



■CUnitForAndo の使用法

1. ソースファイル

---- sq.c ----------------------------------------------
1
2 /** 被テスト関数 */
3 int sq(int a) {
4 return a * a;
5 }
-----------------------------------------------------------

この関数をテストします。


2. テスト・ドライバーの準備

---- sq_test.c ------------------------------------------
1 #include <stdio.h>
2 #include <testRunner.h>
3
4 int sq(int);
5 static unsigned int test01(void);
6 static unsigned int test02(void);
7 static unsigned int test03(void);
8
9 /** Main function. */
10 int main(void) {
11 testRunner(test01);
12 testRunner(test02);
13 testRunner(test03);
14 return 0;
15 }
16
17 /** test case */
18 unsigned int test01(void) {
19 TEST_ASSERT_EQUALS(0, sq(0));
20 return 0;
21 }
22
23 unsigned int test02(void) {
24 TEST_ASSERT_EQUALS(1, sq(1));
25 return 0;
26 }
27
28 unsigned int test03(void) {
29 TEST_ASSERT_EQUALS(2, sq(2)); // これは誤りになる
30 return 0;
31 }
-----------------------------------------------------------

■テストの実行

1. コンパイル+連係編集

ソースファイルは、CUnitForAndo をインストールしたディレクトリーとは別にあります。

$ gcc -c sq.c

$ gcc -ggdb -Wall -I../CUnitForAndo/CUnitForAndo/include
-I../CUnitForAndo/CUnitForAndo/include -c sq_test.c

$ gcc -ggdb -Wall -I../CUnitForAndo/CUnitForAndo/include
-I../CUnitForAndo/CUnitForAndo/include -c testRunner.c
# なお、testRunner.c もコンパイル/リンクする必要があります。

$ gcc sq.o sq_test.o testRunner.o

(注)必要に応じて、インクルード・ファイルの位置を変えてください。


2. テスト

$ ./a.out
OK (1 tests)
OK (1 tests)
sq_test.c:29: error: TestCaseError<0x00000002><0x00000004>
NG (now 0 ok...)


■その他の情報

./CUnitForAndo/html を参照してください。