CUnitを使おう!(導入編)

「CUnitを使おう!(導入編)」の編集履歴(バックアップ)一覧に戻る

CUnitを使おう!(導入編) - (2013/02/09 (土) 15:36:08) の編集履歴(バックアップ)


 -----------------------------------------

序章
-----------------------------------------
 
CUnitとは...
単体テストを支援する 「テスティング・フレームワーク」である
「xUnit」のC言語バージョンです。
 
詳細設計が終了すれば単体テストを行いますが
それらの単体テストの集合体(テストスイート)をCUnitにて作成・実行することが可能となります。
 
 
xUnit Wiki
http://ja.wikipedia.org/wiki/XUnit
 
世界はオブジェクトの海に浮かぶ(CUnit)
http://www.shoeisha.com/mag/windev/pdf/870607/windev0607_113_Object.pdf
 
 
-----------------------------------------
導入
-----------------------------------------
 
1. cygwin を以下からDL
http://www.cygwin.com/
 
 
2. cygwin インストール
→gcc make を追加... 他はデフォルトまま
 
 
3. CUnit を以下からDL
http://sourceforge.net/projects/cunit/
 
→CUnit-2.1-2-src.tar.bz2.tar.bz2.tar.bz2
 
 
4. cygwin 起動
 
5.解凍
$ tar -xvf CUnit-2.1-2-src.tar.bz2.tar.bz2.tar.bz2
→CUnit-2.1-2/が作成される
 
 
6.、ビルド&インストール
 
$./configure --enable-curses
...(--enable-curses オプション:カーソルキーを用いたテキストベースのインタラクティブな ユーザーインターフェースを提供してくれます。)
$make
...
$make install
...
 
 
これだけで導入完了!!!
 
 
 
-----------------------------------------
使用方法
-----------------------------------------
 
1. 適当な*.cファイル作成
 
 
2. 評価対象の関数ソースを書きます。
 
// 評価対象関数
int hoge( int a )
{
return a;
}
 
3. main()と評価関数を作成
 
// main
int main( void )
{
// test suite
CU_pSuite cu_suite;
 
printf("Hello CUnit Would!!\n");
 
// ------------------
// CUnit 初期化
// ------------------
 
// テストレジストリ 初期化
CU_initialize_registry();
 
// テストスイート登録
cu_suite = CU_add_suite("hoge", NULL, NULL);
 
 
// ------------------
// CUnit テスト登録
// ------------------
// テスト関数1
CU_add_test( cu_suite, "test_hoge_001", test_hoge_001 );
// テスト関数2
CU_add_test( cu_suite, "test_hoge_002", test_hoge_001 );
// テスト関数3
CU_add_test( cu_suite, "test_hoge_003", test_hoge_001 );
 
 
// ------------------
// CUnit 実行
// ------------------
CU_console_run_tests();
CU_cleanup_registry();
 
// 正常
return 0;
}
 
// test 1
void test_hoge_001( void )
{
CU_ASSERT( 1 == hoge( 1 ) );
}
 
// test 2
void test_hoge_002( void )
{
CU_ASSERT( 2 == hoge( 2 ) );
}
 
// test 3
void test_hoge_003( void )
{
CU_ASSERT( 3 == hoge( 3 ) );
}
...みたいな感じで書く
 
[備考]
CU_ASSERT( RESULT );
RESULT=TRUE(成功),FALSE(失敗)
 
 
4. make(cunittest.exeの作成)
$gcc main.c -o cunittest.exe -Wall -L/usr/local/lib -lcunit
 
[備考]
-L:ライブラリ格納先
-l:使用ライブラリ
 
 
4. とりあえず実行してみる
 
$./cunittest.exe
 
>     CUnit - A Unit testing framework for C - Version 2.1-2
>             http://cunit.sourceforge.net/
>
>
>***************** CUNIT CONSOLE - MAIN MENU ******************************
>(R)un  (S)elect  (L)ist  (A)ctivate  (F)ailures  (O)ptions  (H)elp  (Q)uit
>Enter command:
 
と表示され、コマンドを要求される。
 
5. テストの実行
 
4が表示されている状態で
"r"+Enter(run all)
で登録されているスイートが実行される。
 
>Running Suite : hoge
>     Running Test : test_hoge_001
>     Running Test : test_hoge_002
>     Running Test : test_hoge_003
>
>Run Summary:    Type  Total    Ran Passed Failed Inactive
>              suites      1      1    n/a      0        0
>               tests      3      3      3      0        0
>             asserts      3      3      3      0      n/a
>
>Elapsed time =    0.000 seconds
 
 
以上!!!CUnitの導入編でした!!!!!
 
<参考>
CUnit チュートリアル
http://homepage3.nifty.com/kaku-chan/cunit/index.html