cunit xml
2009-11-29 23:40
10 查看
这里主要讲CUnit在Linux平台下的应用。这里有一篇 CUnit测试工具使用 ,另一篇 C单元测试包设计与实现 讲的不错,可以看一下。CUnit的主页是 http://cunit.sourceforge.net/index.html。
CUnit以静态库的形式提供给用户使用,用户编写程序的时候直接链接此静态库就可以了。它提供了一个简单的单元测试框架,并且为常用的数据类型提供了丰富的断言语句支持。
CUnit基本架构
Test Registry
|
------------------------------
| |
Suite '1' . . . . Suite 'N'
| |
--------------- ---------------
| | | |
Test '11' ... Test '1M' Test 'N1' ... Test 'NM'
一 次测试(Test Registry)可以运行多个测试包(Test Suite),而每个测试包可以包括多个测试用例(Test Case),每个测试用例又包含一个或者多个断言类的语句。具体到程序的结构上,一次测试下辖多个Test Suite,它对应于程序中各个独立模块;一个Suite管理多个Test Case,它对应于模块内部函数实现。每个Suite可以含有setup和teardown函数,分别在执行suite的前后调用。
CUnit测试模式
CUnit使用四种不同的接口,供用户来运行测试和汇报测试结果:
自动输出到XML文件, 非交互式
基本扩展编程方式, 非交互式
控制台方式, 交互式
Curses图形接口, 交互式
注意1和2是非交互式的,4只能在Unix下使用,常用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()
CUnit使用范例
CUnit的在线文档是 http://cunit.sourceforge.net/doc/index.html ,上面有着详细的论述。这里以使用自动产生XML文件的接口为例,讲述CUnit-2.1-0在Linux平台下的使用。
我要测试的是整数求最大值的函数maxi,我使用如下文件组织结构:
func.c :定义maxi()函数
test_func.c :定义测试用例和测试包
run_test.c :调用CUnit的Automated接口运行测试
Makefile :生成测试程序。
这样组织的好处是,我们可以把各个功能分离,当要改变待测试函数的定义的时候,我们只需要修改func.c,而要增减、修改测试用例,只修改test_func.c就可以了,要使用CUnit提供的别的API,那就修改run_test.c。
它们的内容分别如下所示:
1) func.c
int maxi(int i, int j)
{
// return i>j?i:j; (屏蔽此出得到错误结果)
return i;
}
2) test_func.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "CUnit/CUnit.h"
#include "CUnit/Automated.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(EXIT_FAILURE);
}
}
3) run_test.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main( int argc, char *argv[] )
{
if(CU_initialize_registry()){
fprintf(stderr, " Initialization of Test Registry failed. ");
exit(EXIT_FAILURE);
}else{
AddTests();
CU_set_output_filename("TestMax");
CU_list_tests_to_file();
CU_automated_run_tests();
CU_cleanup_registry();
}
return 0;
}
4) 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 -lcurses -static $^
如果在编译过程中报下面类似错误:
Main.c:(.text+0x245): undefined reference to `CU_initialize_registry'
Main.c:(.text+0x267): undefined reference to `CU_basic_set_mode'
………………(省略)
testcase.c:(.text+0x2f9): undefined reference to `CU_register_suites'
testcase.c:(.text+0x302): undefined reference to `CU_get_error_msg'
这个好像是CUnit的bug, 编译测试用例的时候把源代码和CUnit的.o文件一起编译就没问题了,过程入下:
tar ......(解压)
./configure
make
find . -type f|grep "/.o"|xargs -IXX cp XX /home/test/lib(把编译生成的.o 保存到你喜欢的地方)
编译, 链接时加上这句话: `ls /object/file/destination/*.o` 比如
gcc -o test -I/usr/local/include -L/usr/local/lib -lcunit -lcurses -static run_test.c test_func.c func.c `ls /home/test/lib/*.o`
或者直接只链接lcunit也可以编译通过
gcc -o test -I/usr/local/include -L/usr/local/lib -lcunit run_test.c test_func.c func.c
测试报告
运行上面产生的test程序,会在当前目录下产生两个xml文件:
TestMax-Listing.xml :对测试用例的报告
TestMax-Results.xml :对测试结果的报告
要查看这两个文件,需要使用如下xsl和dtd文件:CUnit-List.dtd和CUnit-List.xsl用于解析列表文件, CUnit-Run.dtd和CUnit-Run.xsl用于解析结果文件。这四个文件在CUnit包里面有提供,安装之后在$(PREFIX) /share/CUnit目录下,默认安装的话在/home/lirui/local/share/CUnit目录下。在查看结果之前,需要把这六个文件:TestMax-Listing.xml, TestMax-Results.xml, CUnit-List.dtd, CUnit-List.xsl, CUnit-Run.dtd, CUnit-Run.xsl拷贝到一个目录下,然后用浏览器打开两个结果的xml文件就可以了。
1) TestMax-Listing.xml在firefox当中显示如下:
2) TestMax-Results.xml在IE当中显示如下:
CUnit以静态库的形式提供给用户使用,用户编写程序的时候直接链接此静态库就可以了。它提供了一个简单的单元测试框架,并且为常用的数据类型提供了丰富的断言语句支持。
CUnit基本架构
Test Registry
|
------------------------------
| |
Suite '1' . . . . Suite 'N'
| |
--------------- ---------------
| | | |
Test '11' ... Test '1M' Test 'N1' ... Test 'NM'
一 次测试(Test Registry)可以运行多个测试包(Test Suite),而每个测试包可以包括多个测试用例(Test Case),每个测试用例又包含一个或者多个断言类的语句。具体到程序的结构上,一次测试下辖多个Test Suite,它对应于程序中各个独立模块;一个Suite管理多个Test Case,它对应于模块内部函数实现。每个Suite可以含有setup和teardown函数,分别在执行suite的前后调用。
CUnit测试模式
CUnit使用四种不同的接口,供用户来运行测试和汇报测试结果:
自动输出到XML文件, 非交互式
基本扩展编程方式, 非交互式
控制台方式, 交互式
Curses图形接口, 交互式
注意1和2是非交互式的,4只能在Unix下使用,常用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()
CUnit使用范例
CUnit的在线文档是 http://cunit.sourceforge.net/doc/index.html ,上面有着详细的论述。这里以使用自动产生XML文件的接口为例,讲述CUnit-2.1-0在Linux平台下的使用。
我要测试的是整数求最大值的函数maxi,我使用如下文件组织结构:
func.c :定义maxi()函数
test_func.c :定义测试用例和测试包
run_test.c :调用CUnit的Automated接口运行测试
Makefile :生成测试程序。
这样组织的好处是,我们可以把各个功能分离,当要改变待测试函数的定义的时候,我们只需要修改func.c,而要增减、修改测试用例,只修改test_func.c就可以了,要使用CUnit提供的别的API,那就修改run_test.c。
它们的内容分别如下所示:
1) func.c
int maxi(int i, int j)
{
// return i>j?i:j; (屏蔽此出得到错误结果)
return i;
}
2) test_func.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "CUnit/CUnit.h"
#include "CUnit/Automated.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(EXIT_FAILURE);
}
}
3) run_test.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main( int argc, char *argv[] )
{
if(CU_initialize_registry()){
fprintf(stderr, " Initialization of Test Registry failed. ");
exit(EXIT_FAILURE);
}else{
AddTests();
CU_set_output_filename("TestMax");
CU_list_tests_to_file();
CU_automated_run_tests();
CU_cleanup_registry();
}
return 0;
}
4) 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 -lcurses -static $^
如果在编译过程中报下面类似错误:
Main.c:(.text+0x245): undefined reference to `CU_initialize_registry'
Main.c:(.text+0x267): undefined reference to `CU_basic_set_mode'
………………(省略)
testcase.c:(.text+0x2f9): undefined reference to `CU_register_suites'
testcase.c:(.text+0x302): undefined reference to `CU_get_error_msg'
这个好像是CUnit的bug, 编译测试用例的时候把源代码和CUnit的.o文件一起编译就没问题了,过程入下:
tar ......(解压)
./configure
make
find . -type f|grep "/.o"|xargs -IXX cp XX /home/test/lib(把编译生成的.o 保存到你喜欢的地方)
编译, 链接时加上这句话: `ls /object/file/destination/*.o` 比如
gcc -o test -I/usr/local/include -L/usr/local/lib -lcunit -lcurses -static run_test.c test_func.c func.c `ls /home/test/lib/*.o`
或者直接只链接lcunit也可以编译通过
gcc -o test -I/usr/local/include -L/usr/local/lib -lcunit run_test.c test_func.c func.c
测试报告
运行上面产生的test程序,会在当前目录下产生两个xml文件:
TestMax-Listing.xml :对测试用例的报告
TestMax-Results.xml :对测试结果的报告
要查看这两个文件,需要使用如下xsl和dtd文件:CUnit-List.dtd和CUnit-List.xsl用于解析列表文件, CUnit-Run.dtd和CUnit-Run.xsl用于解析结果文件。这四个文件在CUnit包里面有提供,安装之后在$(PREFIX) /share/CUnit目录下,默认安装的话在/home/lirui/local/share/CUnit目录下。在查看结果之前,需要把这六个文件:TestMax-Listing.xml, TestMax-Results.xml, CUnit-List.dtd, CUnit-List.xsl, CUnit-Run.dtd, CUnit-Run.xsl拷贝到一个目录下,然后用浏览器打开两个结果的xml文件就可以了。
1) TestMax-Listing.xml在firefox当中显示如下:
2) TestMax-Results.xml在IE当中显示如下:
相关文章推荐
- DotNetCore跨平台~xUnit生成xml报告
- VS2012 Unit Test —— 我对IdleTest库动的大手术以及对Xml相关操作进行测试的方式
- Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用
- phpunit.xml 用法
- hadoop单元调试:MRunit(maven配置pom.xml)
- ant+dbunit 导xml到mysql
- 将 xunit.runner.dnx 的 xml 输出转换为 Nunit 格式
- phpunit.xml 用法
- phpunit.xml
- Visual Studio2010中unittest的xml文件路径error问题
- 将NoseXUnit生成的XML以报表的形式显示~
- entityengine.xml配置mssql问题
- 使用 JavaScript E4X 高效处理 XML
- log4j 1.2 配置 xml
- 解决Eclipse项目中有sql-map-config.xml文件但是运行Junit测试ibatis时提示找不到该文件的一种方法
- eclipse编辑web.xml很卡的原因及解决办法
- Ant build.xml中的各种变量
- iOS之XML解析
- asp.net实现输出xml的方法
- Spring最小化xml配置二之注解方式@Autowired