CUnit使用详解

最近在接触CUnit,写个总结。主要参考文档是:

http://blog.csdn.net/huhongfei/article/details/5870887

http://blog.chinaunix.net/uid-13182088-id-2904570.html

CUnit 的readme文件

1. CUnit安装

1)   下载CUnit源码包(CUnit-2.1-0-src.tar.gz)

2)   CUnit源码包的解压。
输入如下命令:
#tar xzvf CUnit-2.1-0-src.tar.gz
执行结束后,将会在当前目录下生成一个解压后的文件夹(CUnit-2.1-0)。

3)   解压结束后,开始进行编译和安装。
#su     变换为root

#cd CUnit-2.1-0
#aclocal 
#autoconf 
#automake 
#chmod u+x configure
#./configure --prefix <Your choice of directory for installation>
(对上一句进行解释,<Your choice of directory for installation>这个位置,需要你输入要安装的目录,目录的格式举例如下:/usr/unittest/)
#make (可能会编译出错,如果是”../libtool: line 818: X--tag=CC: command not found”类似的错误,解决办法是“目录下找到libtool,把里面的$echo全部替换为$ECHO”,方法是gvim :%s/$echo/$ECHO/g)
#make install
这里需要一段时间...
#cd /usr/unittest/lib
#ldconfig

2. 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的前后调用。

3. CUnit输出方式

1)       直接输出到xml方式

CU_set_output_filename("TestMax");
CU_list_tests_to_file();
CU_automated_run_tests();

2)       输出到console

CU_basic_set_mode(CU_BRM_VERBOSE);

CU_basic_run_tests();

 4. 断言

断言都定义在CUnit.h中,常用的断言如下(所有的都有FATAL版本,表示如果出错则退出整个程序):

断言

作用

CU_PASS(msg)

做一条“通过”的断言

CU_FAIL(msg)

故意做一条“错误”的断言(todo比较有用)

CU_TEST(value)

测试条件

CU_ASSERT_TRUE(value)

断言正确

CU_ASSERT_FALSE(value)

断言错误

CU_ASSERT_EQUAL(value)

断言相等

CU_ASSERT_PTR_EQUAL(actual, expected)

断言指针指向同一区域

CU_ASSERT_PTR_NOT_EQUAL(actual, expected)

断言指针指向不同区域

CU_ASSERT_STRING_EQUAL(actual, expected)

断言字符串内容相等

CU_ASSERT_STRING_NOT_EQUAL(actual, expected)

断言字符串内容不相等

CU_ASSERT_DOUBLE_EQUAL(actual, expected, granularity)

断言double actual == expected within the specified tolerance.

CU_ASSERT_DOUBLE_NOT_EQUAL(actual, expected, granularity)

断言double actual != expected within the specified tolerance.

5. 示例

下面有三个文件,func.c用于定义正式函数。Func_test.c用于定义suite并将测试加入到suite中。Run_test.c是主程序,除非想要用不同的输出方式,一般不用动。

---------

func.c
--------

int maxi(int i, int j)
{
    return i>j?i:j;
//        return i;
}

--------
test_func.c
--------

View Code

--------
run_test.c
--------

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int main( int argc, char *argv[] )
{
       printf("hello");
       if(CU_initialize_registry()){
                fprintf(stderr, " Initialization of Test Registry failed. ");
                exit(EXIT_FAILURE);
        }else{
                //调用func_test中的布置suites和tests的函数 
                AddTests();

                CU_basic_set_mode(CU_BRM_VERBOSE);
            CU_basic_run_tests();
        CU_cleanup_registry();
        }
        return 0;
}

6. 编译和测试过程

编译过程:
#gcc  -o test  -I/usr/unittest/include  -L/usr/unittest/lib  -lcunit  run_test.c  test_func.c  func.c

运行过程:

#export LD_LIBRARY_PATH=/usr/unittest/lib

#./test

为了方便运行,我编写了一个脚本。直接运行脚本即可。

View Code
# this script is used for debug or run a unit test based on CUnit.
# you can revise the variables blow to change the default behavior.

# usage 
# $: unittest {-b} 
# if -b is identified, the script will only do the build work . By default, it will not be identified.

#if you donot want the process to be printed on shell, use 'set +x'
set +x

################################
# declaration of variables
################################
# current location
CURRENT_DIR=`pwd`

# directiorys installing CUnit, which is indicated by '--prefix' when using './configure ...' to install CUnit
CUNIT_ROOT_DIRS=/home/aicro/project/CUnit  

# directions for CUnit include files
CUNIT_INCLUDE_FILES_DIR=$CUNIT_ROOT_DIRS/include 

# directions for CUnit lib files
CUNIT_LIB_FILES_DIR=$CUNIT_ROOT_DIRS/lib 

# extra include files
# should be like '-I/path/name -I/path/name2'
INCLUDE_FILES_DIR=
#extra lib files
# should be like '-L/path/name -L/path/name2'
LIB_FILES_DIR=

######################################
# check parameters
######################################
NEED_RUN=1

while getopts 'b' OPT; do
        case $OPT in
                b)
                NEED_RUN=0;;
                
                ?)
                echo "Unknown parameters"
        esac
done

##################################
# find all the .h and .c, which will later join the building process 
##################################
gcc  -o test  -I$CUNIT_INCLUDE_FILES_DIR $INCLUDE_FILES_DIR  -L$CUNIT_LIB_FILES_DIR $LIB_FILES_DIR -lcunit `ls *\.c` 

###################################
# if gvim has been closed, restore the old vimrc
###################################
if [ "$NEED_RUN" -eq "1" ]; then
    export LD_LIBRARY_PATH=$CUNIT_ROOT_DIRS/lib
    ./test
fi
原文地址:https://www.cnblogs.com/aicro/p/2932179.html