您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 >
CUnit Framework 介紹
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/3/15 13:56:13 ] 推薦標(biāo)簽:

      1 CUnit Framework介紹
      繼Junit CppUnit的成功后, c語言環(huán)境下也出現(xiàn)了開發(fā)源碼的白盒測試用例CUnit。CUnit以靜態(tài)庫的形式提供給用戶使用,用戶編寫程序的時(shí)候直接鏈接此靜態(tài)庫可以了。它提供了一個(gè)簡單的單元測試框架,并且為常用的數(shù)據(jù)類型提供了豐富的斷言語句支持。下面介紹一下CUnit結(jié)構(gòu)框架和具體使用:

       1.1 結(jié)構(gòu)框架
       在CUnit的主頁上可以看到對他結(jié)構(gòu)簡單描述

          Test Registry
              |
------------------------------
          |         |
      Suite '1' . Suite 'N'
          |         |
--------------- ---------------
       |    |     |    |
Test '11' ... Test '1M' Test 'N1' ... Test 'NM'

       CUnit的測試是單線程啟動,只能注冊一個(gè)測試用例Test Registry, 一次測試(Test Registry)可以運(yùn)行多個(gè)測試包(Test Suite),而每個(gè)測試包可以包括多個(gè)測試用例(Test Case),每個(gè)測試用例又包含一個(gè)或者多個(gè)斷言類的語句。具體到程序的結(jié)構(gòu)上,一次測試下轄多個(gè)Test Suite,它對應(yīng)于程序中各個(gè)獨(dú)立模塊;一個(gè)Suite管理多個(gè)Test Case,它對應(yīng)于模塊內(nèi)部函數(shù)實(shí)現(xiàn)。每個(gè)Suite可以含有setup和teardown函數(shù),分別在執(zhí)行suite的前后調(diào)用。
       注冊一個(gè)測試用例(如果已經(jīng)注冊了你可以cleanup掉然后重新注冊使用)然后CU_add_suite增加你的模塊然后CU_add_test再在你的模塊下掛載你的模塊內(nèi)的測試函數(shù)。所有的掛載完畢后,調(diào)用你想使用的界面進(jìn)行測試。

        1.2 測試模式
        下面是四種測試模式:
        1 Automated Output to xml file Non-interactive
        2 Basic Flexible programming interface Non-interactive
        3 Console Console interface (ansi C) Interactive
        4 Curses Graphical interface (Unix) Interact
        注意1,2是沒有交互功能的,4的Unix下的我是windows用戶, 選擇第3個(gè)介紹console而且console是可以人機(jī)交互的。
        1.3 測試基本流程
        使用CUnit進(jìn)行測試的基本流程如下所示:
        1.書寫代測試的函數(shù)(如果必要,需要寫suite的init/cleanup函數(shù))
        2.初始化Test Registry - CU_initialize_registry()
        3.把測試包(Test Suites)加入到Test Registry - CU_add_suite()
        4.加入測試用例(Test Case)到測試包當(dāng)中 - CU_add_test()
        5.使用適當(dāng)?shù)慕涌趤磉\(yùn)行測試測試程序,例如 CU_console_run_tests()
        6.清除Test Registry - CU_cleanup_registry()

        1.4 TestCase的構(gòu)成
        一個(gè)CUnit TestCase的構(gòu)成有以下文件:test.c、testcase.c、Main.c 與 Makefile構(gòu)成。即一個(gè)測試范例由①被測函數(shù),②測試函數(shù)(定義策識用例和測試包),③運(yùn)行測試函數(shù)及④Makefile四部分構(gòu)成。


        2 CUnit Framework的安裝

        2.1 CUnit Framework的下載
        Cunit Framework的當(dāng)前版本為:CUnit-2.1-0-src.tar.gz。

        2.2 CUnit Framework的安裝
        CUnit的Framework的安裝環(huán)境為Fedora 5。安裝命令如下:
        (1)解包
#tar xzvf CUnit-2.1-0-src.tar.gz

        (2)編譯與安裝
#cd CUnit-2.1-0
#aclocal (if necessary)
#autoconf (if necessary)
#automake (if necessary)
#chmod u+x configure (if necessary)
#./configure --prefix <Your choice of directory for installation>
#make
#make install

        (3)加載CUnit的庫(only one time)
#cd /usr/local/lib
#ldconfig


        3 CUnit TestCase構(gòu)成
        3.1 CUnit TestCase的構(gòu)成
        一個(gè)CUnit TestCase的構(gòu)成有以下文件:test.c、testcase.c、Main.c 與 Makefile構(gòu)成。

        3.2 CUnit TestCase主要構(gòu)成函數(shù)說明
        以下以一個(gè)簡單的testcase為例說明。
        我要測試的是整數(shù)求大值的函數(shù)maxi,我使用如下文件組織結(jié)構(gòu):
        1.test.c:被測函數(shù)(定義maxi()函數(shù))
        2.testcase.c:測試函數(shù)(定義測試用例和測試包)
        3.Main.c:運(yùn)行測試函數(shù)(調(diào)用CUnit的Automated接口運(yùn)行測試)
        4.Makefile :生成測試程序。

       (1)被測函數(shù)test.c
/**
*file: test.c
**/
int maxi(int i,int j)
{
return i>j?i:j;
}

     (2)測試函數(shù)(定義策識用例和測試包)testcase.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/CUnit.h>
#include <CUnit/Automated.h>
#include <CUnit/TestDB.h>
/**//*---- functions to be tested ------*/
extern int maxi(int i, int j);

/**//*---- test cases ------------------*/
void testIQJ()
{
CU_ASSERT_EQUAL(maxi(1,1),1);
CU_ASSERT_EQUAL(maxi(0,-0),0);
}
void testIGJ()
{
CU_ASSERT_EQUAL(maxi(2,1),2);
CU_ASSERT_EQUAL(maxi(0,-1),0);
CU_ASSERT_EQUAL(maxi(-1,-2),-1);
}
void testILJ()
{
CU_ASSERT_EQUAL(maxi(1,2),2);
CU_ASSERT_EQUAL(maxi(-1,0),0);
CU_ASSERT_EQUAL(maxi(-2,-1),-1);
}

CU_TestInfo testcases[] = {
{"Testing i equals j:", testIQJ},
{"Testing i greater than j:", testIGJ},
{"Testing i less than j:", testILJ},
CU_TEST_INFO_NULL
};

/**//*---- test suites ------------------*/
int suite_success_init(void)
{ return 0; }

int suite_success_clean(void)
{ return 0; }

CU_SuiteInfo suites[] = {
{"Testing the function maxi:", suite_success_init, suite_success_clean, testcases},
CU_SUITE_INFO_NULL
};

/**//*---- setting enviroment -----------*/

void AddTests(void)
{
assert(NULL != CU_get_registry());
assert(!CU_is_test_running());
/**//* shortcut regitry */

if(CUE_SUCCESS != CU_register_suites(suites)){
fprintf(stderr, "Register suites failed - %s ", CU_get_error_msg());
exit(EXIT_FAILURE);
}
}

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd