CUnit

繼Junit CppUnit的成功後, c語言環境下也出現了開放源碼的白盒測試用例CUnit。CUnit以靜態庫的形式提供給用戶使用,用戶編寫程式的時候直接連結此靜態庫就可以了。它提供了一個簡單的單元測試框架,並且為常用的數據類型提供了豐富的斷言語句支持。

基本介紹

  • 中文名:CUnit
  • 屬於:C語言單元測試框架
  • 類型:源碼的白盒測試用例
  • 形式:靜態庫
簡介,結構框架,測試模式,測試流程,構成,CUnit TestCase的構成,主要構成函式說明,測試程式,下載,安裝,解包,編譯與安裝,

簡介

CUnit是一種C語言單元測試框架 ,繼Junit CppUnit的成功後, c語言環境下也出現了開放源碼的白盒測試用例CUnit。CUnit以靜態庫的形式提供給用戶使用,用戶編寫程式的時候直接連結此靜態庫就可以了。它提供了一個簡單的單元測試框架,並且為常用的數據類型提供了豐富的斷言語句支持。

結構框架

CUnit的測試是單執行緒啟動,只能註冊一個Test Registry, 一次測試(Test Registry)可以運行多個測試包(Test Suite),而每個測試包可以包括多個測試用例(Test Case),每個測試用例又包含一個或者多個斷言類的語句。具體到程式的結構上,一次測試下轄多個Test Suite,它對應於程式中各個獨立模組;一個Suite管理多個Test Case,它對應於模組內部函式實現。每個Suite可以含有setup和teardown函式,分別在執行suite的前後調用。
註冊一個測試用例(如果已經註冊了你可以cleanup掉然後重新註冊使用)然後CU_add_suite增加你的模組然後CU_add_test再在你的模組下掛載你的模組內的測試函式。所有的掛載完畢後,調用你想使用的界面進行測試。

測試模式

下面是四種測試模式:
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個介紹console而且console是可以人機互動的。

測試流程

使用CUnit進行測試的基本流程如下所示:
⒈書寫代測試的函式(如果必要,需要寫suite的init/cleanup函式)
⒉初始化Test Registry - CU_initialize_registry()
⒊把測試包(Test Suites)加入到Test Registry - CU_add_suite()
⒋加入測試用例(Test Case)到測試包當中 - CU_add_test()
⒌使用適當的接口來運行測試測試程式,例如 CU_console_run_tests()
⒍清除Test Registry - CU_cleanup_registry()
⒈4 TestCase的構成
一個CUnit TestCase的構成有以下檔案:test.c、testcase.c、Main.c 與 Makefile構成。即一個測試範例由①被測函式,②測試函式(定義測試用例測試包),③運行測試函式及④Makefile四部分構成。

構成

CUnit TestCase的構成

一個CUnit TestCase的構成有以下檔案:test.c、testcase.c、Main.c 與 Makefile構成。

主要構成函式說明

以下以一個簡單的testcase為例說明。
我要測試的是整數求最大值的函式maxi,我使用如下檔案組織結構:
⒈test.c:被測函式(定義maxi()函式)
⒉testcase.c:測試函式(定義測試用例測試包
⒊Main.c:運行測試函式(調用CUnit的Automated接口運行測試)

測試程式

⑴被測函式test.c
/**
*file:test.c
**/
int maxi(int i,int j)
{
return i>j?i:j;
}
測試函式(定義測試用例測試包)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(EⅪT_FAILURE);
}
}
⑶運行測試函式 Main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "Basic.h"
int main(int argc, char* argv[])
{
CU_BasicRunMode mode = CU_BRM_VERBOSE;
CU_ErrorAction error_action = CUEA_IGNORE;
int i;
setvbuf(stdout, NULL, _IONBF, 0);
for (i=1 ; i<argc ; i++) {
if (!strcmp("-i", argv)) {
error_action = CUEA_IGNORE;
}
else if (!strcmp("-f", argv)) {
error_action = CUEA_FAIL;
}
else if (!strcmp("-A", argv)) {
error_action = CUEA_ABORT;
}
else if (!strcmp("-s", argv)) {
mode = CU_BRM_SILENT;
}
else if (!strcmp("-n", argv)) {
mode = CU_BRM_NORMAL;
}
else if (!strcmp("-v", argv)) {
mode = CU_BRM_VERBOSE;
}
else if (!strcmp("-e", argv)) {
return 0;
}
else {
printf("\nUsage:BasicTest [options]\n\n"
"Options:-i ignore framework errors [default].\n"
" -f fail on framework error.\n"
" -A abort on framework error.\n\n"
" -s silent mode - no output to screen.\n"
" -n normal mode - standard output to screen.\n"
" -v verbose mode - max output to screen [default].\n\n"
" -e print expected test results and exit.\n"
" -h print this message and exit.\n\n");
return 0;
}
}
if (CU_initialize_registry()) {
printf("\nInitialization of Test Registry failed.");
}
else {
AddTests();
CU_basic_set_mode(mode);
CU_set_error_action(error_action);
printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
CU_cleanup_registry();
}
return 0;
}
⑷Makefile
INC=-I/usr/local/include/CUnit
LIB=-L/usr/local/lib/
all:func.c test_func.c run_test.c
#gcc -o test $(INC) $(LIB) -lcunit $^
gcc -o test $(INC) $(LIB) -lcunit  $^
clean:
-rm -rf *.o test

下載

CUnit Framework的下載
Cunit Framework的當前版本為:CUnit-2.1-3.tar.bz2。

安裝

CUnit的Framework的安裝環境為Fedora 5。安裝命令如下:

解包

#tar xzvf CUnit-2.1-0-src.tar.gz

編譯與安裝

#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
⑶載入CUnit的庫(only one time)
#cd /usr/local/lib
#ldconfig

相關詞條

熱門詞條

聯絡我們