什么是CUnit

核心提示CUnit是在c语言环境下的一种开放源码的白盒测试框架。外文名CUnit类型源码的白盒测试用例形式静态库CUnit简介CUnit是一种C语言单元测试框架 ,CUnit以静态库的形式提供给用户使用,用户编写程序的时候直接链接此静态库就可以了。

最佳答案:

CUnit是在c语言环境下的一种开放源码的白盒测试框架。

详情介绍

CUnit是在c语言环境下的一种开放源码的白盒测试框架。

外文名
CUnit
类型
源码的白盒测试用例
形式
静态库

CUnit简介

CUnit是一种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再在你的模块下挂载你的模块内的测试函数。所有的挂载完毕后,调用你想使用的界面进行测试。

CUnit测试模式

下面是四种测试模式:

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测试流程

使用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构成

CUnitCUnit TestCase的构成

一个CUnit TestCase的构成有以下文件:test.c、testcase.c、Main.c 与 Makefile构成。

CUnit主要构成函数说明

以下以一个简单的testcase为例说明。

我要测试的是整数求最大值的函数maxi,我使用如下文件组织结构:

⒈test.c:被测函数(定义maxi()函数)

⒉testcase.c:测试函数(定义测试用例和测试包)

⒊Main.c:运行测试函数(调用CUnit的Automated接口运行测试)

CUnit测试程序

⑴被测函数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>

extern int maxi(int i, int j);

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

};

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

};

void AddTests(void)

{

assert(NULL != CU_get_registry());

assert(!CU_is_test_running());

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 nn"

"Options:-i ignore framework errors .n"

" -f fail on framework error.n"

" -A abort on framework error.nn"

" -s silent mode - no output to screen.n"

" -n normal mode - standard output to screen.n"

" -v verbose mode - max output to screen .nn"

" -e print expected test results and exit.n"

" -h print this message and exit.nn");

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下载

CUnit framework的下载

Cunit framework的当前版本为:CUnit-2.1-3.tar.bz2。

CUnit安装

CUnit的framework的安装环境为Fedora 5。安装命令如下:

CUnit解包

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

CUnit编译与安装

#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

 
友情链接
鄂ICP备19019357号-22