关于CUnit的安装请自行百度
我的系统:fedora22 64bit
我的CUnit的头文件在:/usr/include/CUnit/
库文件在:/usr/lib64/
文件:
[laolang@laolang mycunittest]$ tree
.
├── cal.c
├── cal.h
├── Makefile
├── test.c
└── testcal.c
0 directories, 5 files
[laolang@laolang mycunittest]$
代码:
cal.h
[laolang@laolang mycunittest]$ cat cal.h
#ifndef _CAL_H_
#define _CAL_H_
int add(int a, int b );
int minus( int a, int b);
#endif
[laolang@laolang mycunittest]$
cal.c
[laolang@laolang mycunittest]$ cat cal.c
#include"cal.h"
int add(int a, int b ){
return a + b;
}
int minus( int a, int b){
return a - b;
}
[laolang@laolang mycunittest]$
testcal.c
[laolang@laolang mycunittest]$ cat testcal.c
#include <stdio.h>
#include <assert.h>
#include <CUnit/Console.h>
#include "cal.h"
int InitSuite()
{
return 0;
}
int EndSuite()
{
return 0;
}
int test_add(int a, int b, int real)
{
int result;
result = add(a, b);
if(result == real)
{
return 1;
}
return 0;
}
int test_minus(int a, int b, int real)
{
int result;
result = minus(a, b);
if(result == real)
{
return 1;
}
return 0;
}
void TestAdd()
{
// CU_ASSERT(test_add(3, 4, 7));
int result = add(3, 4);
int real = 7;
CU_ASSERT_EQUAL(result,real);
}
void TestMinus()
{
// CU_ASSERT(test_minus(4, 5, -1));
int result = minus(3, 4);
int real = -1;
CU_ASSERT_EQUAL(result,real);
}
/*0 表示成功,1表示失败*/
int AddTestCalModule()
{
CU_pSuite pSuite = NULL;
/***************
* 1. CU_add_suite 增加一个Suite
* 2. Suite名字 : testSuite
* 3. InitSuite EndSuite:分别是测试单元初始和释放函数,如不需要则NULL传递
****************/
pSuite = CU_add_suite("cal模块", InitSuite, EndSuite);
//检测注册Suite情况
if(NULL == pSuite)
{
//return 1;
}
/***************
* 1. 注册当前Suite下的测试用例
* 2. pSuite:用例指针
* 3. "Test1": 测试单元名称
* 4. Test1:测试函数
***************/
if( NULL == CU_add_test(pSuite, "add(加)", TestAdd) ||
NULL == CU_add_test(pSuite, "minus(减)", TestMinus))
{
return 1;
}
/***另外一种测试方式***************/
/*
CU_TestInfo testcases[] = {
{"Test1:", Test1},
{"Test2:", Test2},
CU_TEST_INFO_NULL
};
CU_SuiteInfo suites[] = {
{"Testing the function cal_num:", InitSuite, EndSuite, testcases},
CU_SUITE_INFO_NULL
};
if(CUE_SUCCESS != CU_register_suites(suites))
{
return 1;
}
*/
/************************************/
return 0;
}
[laolang@laolang mycunittest]$
test.c
[laolang@laolang mycunittest]$ cat test.c
#include <stdio.h>
#include <assert.h>
#include <CUnit/Console.h>
extern int AddTestCalModule();
int main()
{
//CU_initialize_registry 注册函数注册一个用例返回CUE_系列异常值
if( CUE_SUCCESS != CU_initialize_registry())
{
return CU_get_error();
}
//CU_get_registry 返回注册到用例指针
assert(NULL != CU_get_registry());
//检测是否在执行
assert(!CU_is_test_running());
//调用测试模块完成测试用例
if (0 != AddTestCalModule())
{
CU_cleanup_registry();
return CU_get_error();
}
//使用console控制交互界面的函数入口
CU_console_run_tests();
/***使用自动产生XML文件的模式********
CU_set_output_filename("TestMax");
CU_list_tests_to_file();
CU_automated_run_tests();
***********************************/
//调用完毕清除注册信息
CU_cleanup_registry();
return 0;
}
[laolang@laolang mycunittest]$
Makefile
laolang@laolang mycunittest]$ cat Makefile
INC=-I/usr/include/CUnit/
LIB=-L/usr/lib64/
#gcc -o test -I /usr/include/CUnit/ -L /usr/lib64/ -lcunit *.c
all: cal.c testcal.c test.c
gcc $^ -o hello $(INC) $(LIB) -lcunit
clean:
rm -rf hello
[laolang@laolang mycunittest]$
运行:
[laolang@laolang mycunittest]$ make
gcc cal.c testcal.c test.c -o hello -I/usr/include/CUnit/ -L/usr/lib64/ -lcunit
[laolang@laolang mycunittest]$ ./hello
CUnit - A Unit testing framework for C - Version 2.1-3
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: r
Running Suite : cal模块
Running Test : add(加)
Running Test : minus(减)
Run Summary: Type Total Ran Passed Failed Inactive
suites 1 1 n/a 0 0
tests 2 2 2 0 0
asserts 2 2 2 0 n/a
Elapsed time = 0.000 seconds
***************** CUNIT CONSOLE - MAIN MENU ******************************
(R)un (S)elect (L)ist (A)ctivate (F)ailures (O)ptions (H)elp (Q)uit
Enter command: l
--------------------- Registered Suites -----------------------------
# Suite Name Init? Cleanup? #Tests Active?
1. cal模块 Yes Yes 2 Yes
---------------------------------------------------------------------
Total Number of Suites : 1
***************** CUNIT CONSOLE - MAIN MENU ******************************
(R)un (S)elect (L)ist (A)ctivate (F)ailures (O)ptions (H)elp (Q)uit
Enter command: s
--------------------- Registered Suites -----------------------------
# Suite Name Init? Cleanup? #Tests Active?
1. cal模块 Yes Yes 2 Yes
---------------------------------------------------------------------
Total Number of Suites : 1
Enter number of suite to select (1-1) : 1-1
Suite 'cal模块' selected.
***************** CUNIT CONSOLE - SUITE MENU ***************************
(R)un (S)elect (L)ist (A)ctivate (F)ailures (U)p (O)ptions (H)elp (Q)uit
Enter command: r
Running Suite : cal模块
Running Test : add(加)
Running Test : minus(减)
Run Summary: Type Total Ran Passed Failed Inactive
suites 1 1 n/a 0 0
tests 2 2 2 0 0
asserts 2 2 2 0 n/a
Elapsed time = 0.000 seconds
***************** CUNIT CONSOLE - SUITE MENU ***************************
(R)un (S)elect (L)ist (A)ctivate (F)ailures (U)p (O)ptions (H)elp (Q)uit
Enter command: u
***************** CUNIT CONSOLE - MAIN MENU ******************************
(R)un (S)elect (L)ist (A)ctivate (F)ailures (O)ptions (H)elp (Q)uit
Enter command: q
[laolang@laolang mycunittest]$
解释:
这个例子参考自:http://blog.csdn.net/scucj/article/details/4385630/
原文用的方法是我注释的那一部分,即写一个test_add方法,再写一个TestAdd方法。不过我觉得这种不如直接在TestAdd方法中调用需要测试的方法
关于CUnit解释的比较好的例子:http://www.cnblogs.com/linux-sir/archive/2012/08/25/2654557.html
然后这个是不能运行的,提示是testcase类型有问题,但是这篇博文的解释还是很好的
我的总结:
1、写你的模块,比如cal
2、每一个模块写一个测试文件,如testcal.c
3、写一个总的测试文件,在此文件中包含需要测试的模块对应的测试文件
现在我把其中的数据修改一下:
void TestMinus()
{
// CU_ASSERT(test_minus(4, 5, -1));
int result = minus(3, 4);
int real = 1;//这里由-1修改为1
CU_ASSERT_EQUAL(result,real);
}
运行效果:
[laolang@laolang mycunittest]$ make
gcc cal.c testcal.c test.c -o hello -I/usr/include/CUnit/ -L/usr/lib64/ -lcunit
[laolang@laolang mycunittest]$ ./hello
CUnit - A Unit testing framework for C - Version 2.1-3
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: r
Running Suite : cal模块
Running Test : add(加)
Running Test : minus(减)
Run Summary: Type Total Ran Passed Failed Inactive
suites 1 1 n/a 0 0
tests 2 2 1 1 0
asserts 2 2 1 1 n/a
Elapsed time = 0.000 seconds
***************** CUNIT CONSOLE - MAIN MENU ******************************
(R)un (S)elect (L)ist (A)ctivate (F)ailures (O)ptions (H)elp (Q)uit
Enter command: f
--------------- Test Run Failures -------------------------
src_file:line# : (suite:test) : failure_condition
1. testcal.c:53 : (cal模块 : minus(减)) : CU_ASSERT_EQUAL(result,real)
-----------------------------------------------------------
Total Number of Failures : 1
***************** CUNIT CONSOLE - MAIN MENU ******************************
(R)un (S)elect (L)ist (A)ctivate (F)ailures (O)ptions (H)elp (Q)uit
Enter command: q
[laolang@laolang mycunittest]$
可以看到如果测试未通过,按F可以看出来是那个测试模块的哪个测试出问题了,还提示了在哪一行,哪一个断言