CUnit-2.1-2-src.tar.bz2下载地址http://sourceforge.net/projects/cunit/?source=dlp
解压压缩包解得到PREFIX/CUnit-2.1-2
进入PREFIX/CUnit-2.1-2
(如果需要,chmod +x configure)
./configure--enable-curses=yes
make
makeinstall(如果权限不足,请用sudo)
默认情况下,CUnit被安装到/usr/local/include/CUnit和/usr/local/lib下面
安装成功后,我们可以利用mancunit获取帮助信息
现在,来测试CUnit
首先,编写一段简单的代码
func.c
int maxi(int i, intj)
{
returni > j ? i : j;
}
编写测试用例和测试驱动(以下代码参考http://blog.csdn.net/colin719/article/details/1420583)
test_func.c
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<CUnit/CUnit.h>
#include<CUnit/Automated.h>
/*---- 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_TestInfotestcases[] = {
{"Testing i equals j:",testIQJ},
{"Testing i greater than j:",testIGJ},
{"Testing i less than j:",testILJ},
CU_TEST_INFO_NULL
};
/*---- test suites------------------*/
intsuite_success_init(void) { return 0; }
intsuite_success_clean(void) { return 0; }
CU_SuiteInfosuites[] = {
{"Testing the functionmaxi:", suite_success_init, suite_success_clean, testcases},
CU_SUITE_INFO_NULL
};
/*---- settingenviroment -----------*/
void AddTests(void)
{
assert(NULL != CU_get_registry());
assert(!CU_is_test_running());
/* shortcut regitry */
if(CUE_SUCCESS !=CU_register_suites(suites)){
fprintf(stderr, "Registersuites failed - %s ", CU_get_error_msg());
exit(EXIT_FAILURE);
}
}
run_test.c
/**
* file: 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;
}
编写Makefile文件
all: func.ctest_func.c run_test.c
@gcc-o test $^ -lcunit -static
编译,make
执行,./test
问题
实验发现必须加上-static选项才行,否则,错误信息如下
icode@icode-G41MT-S2:/mnt/ubuntu/ubuntu_ext/code_work/C$ sudo make
gcc -o test func.o test_func.o run_test.o -lcunit
/usr/local/lib/libcunit.so:对‘echo’未定义的引用
/usr/local/lib/libcunit.so:对‘wattr_on’未定义的引用
/usr/local/lib/libcunit.so:对‘acs_map’未定义的引用
/usr/local/lib/libcunit.so:对‘cbreak’未定义的引用
/usr/local/lib/libcunit.so:对‘waddnstr’未定义的引用
/usr/local/lib/libcunit.so:对‘wborder’未定义的引用
/usr/local/lib/libcunit.so:对‘init_pair’未定义的引用
/usr/local/lib/libcunit.so:对‘wrefresh’未定义的引用
/usr/local/lib/libcunit.so:对‘keypad’未定义的引用
/usr/local/lib/libcunit.so:对‘initscr’未定义的引用
/usr/local/lib/libcunit.so:对‘start_color’未定义的引用
/usr/local/lib/libcunit.so:对‘newwin’未定义的引用
/usr/local/lib/libcunit.so:对‘wattr_off’未定义的引用
/usr/local/lib/libcunit.so:对‘delwin’未定义的引用
/usr/local/lib/libcunit.so:对‘prefresh’未定义的引用
/usr/local/lib/libcunit.so:对‘stdscr’未定义的引用
/usr/local/lib/libcunit.so:对‘scrollok’未定义的引用
/usr/local/lib/libcunit.so:对‘newpad’未定义的引用
/usr/local/lib/libcunit.so:对‘endwin’未定义的引用
/usr/local/lib/libcunit.so:对‘wgetch’未定义的引用
/usr/local/lib/libcunit.so:对‘wclear’未定义的引用
/usr/local/lib/libcunit.so:对‘mvwprintw’未定义的引用
/usr/local/lib/libcunit.so:对‘nocbreak’未定义的引用
/usr/local/lib/libcunit.so:对‘noecho’未定义的引用
/usr/local/lib/libcunit.so:对‘wgetnstr’未定义的引用
/usr/local/lib/libcunit.so:对‘wmove’未定义的引用
/usr/local/lib/libcunit.so:对‘curs_set’未定义的引用
/usr/local/lib/libcunit.so:对‘werase’未定义的引用
collect2: ld returned 1 exit status
make: *** [all] 错误 1
参看上面(http://blog.csdn.net/colin719/article/details/1420583)的内容,如果改为
gcc -o test func.o test_func.o run_test.o -lcunit -lcurses
也是可以的,可见,libcuni.so需要libcurses.so的支持
icode@icode-G41MT-S2:/mnt/ubuntu/ubuntu_ext/code_work/C$ locate libcurses
/usr/lib/i386-linux-gnu/libcurses.a
/usr/lib/i386-linux-gnu/libcurses.so
当前目录下面生成了TestMax-Listing.xml和TestMax-Results.xml两个文件。
我们将PREFIX/CUnit-2.1-2/Share目录下的CUnit-List.dtd、Cunit-List.xsl、CUnit-Run.dtd、CUnit-Run.xsl四个文件拷贝到当前目录下,用浏览器打开生成的两个文件,显示如下
以后,就可以用CUnit进行C的单元测试了!!