C语言单元测试

来源:互联网 发布:胎教讲故事软件 编辑:程序博客网 时间:2024/06/10 02:34

CUnit下载地址: http://sourceforge.net/projects/cunit/
CUnit 在线文档帮助:http://cunit.sourceforge.net/doc/index.html

 

关于CUnit, 本文主要从介绍三方面的内容:
1.CUnit的介绍。
1.1 介绍如何使用CUnit。

CUnit是一个对C语言编写的程序进行单元测试的框架,在线文档说它作为一个静态链接库被链接到用户的测试代码中。它提供了一种简洁的框架来建立测试架构,并提供丰富的断言(Assertion)来测试通用数据类型。除此之外,它还提供了许多不同的结构来运行测试用例和报告测试结果。

(1)CUnit的架构
                      Test Registry
                            |
             ------------------------------
             |                            |
          Suite '1'      . . . .       Suite 'N'
             |                            |
       ---------------             ---------------
       |             |             |             |
    Test '11' ... Test '1M'     Test 'N1' ... Test 'NM'

提到这个框架,为后面如何使用CUnit提供了基础。
先介绍这个框架,从底层往上介绍就两句话:
(1)每个测试用例被包装在一个测试包(suite)中,
(2)每个测试包(suite)是在有效的测试注册单元(Test Registry)中注册的。

对于CUnit来说,它是单线程运行,所以每次只能有一个有效的测试注册单元(Test Registry),这个测试注册单元下面可以包含多个测试包(suite),每个测试包可以拥有多个测试用例。划分测试包(suite)的规则可以自由约定,比如按照模块来划分,一个模块的测试用例集中到一个测试包中(suite)。至于测试用例,则用来测试模块内部的函数。测试用例函数通过提供的各类输入调用被测试的函数,返回执行结果,然后通过CUnit提供的断言来判断被测试的函数是否正确。

(2)测试模式

下面是四种测试模式:
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)     Interactive
第一种模式是将结果输出到XML文档中,便于生成报告。第二种模式是每一次运行结束之后在standard output中显示测试结果,不能保留测试结果数据。第三种模式是console方式的,可以人机交互;前两种模式是非交互式的。第四种只在Unix中使用。

(3)测试的基本流程
1)编写单元测试函数(有必要的话要写suite的init/cleanup函数)。Write functions for tests (and suite init/cleanup if necessary).
2)调用函数CU_initialize_registry()初始化测试注册单元(Test Registry)。 Initialize the test registry - CU_initialize_registry()
3)调用函数CU_add_suite() 将测试包(suite)添加到测试注册单元(Test Registry)中。Add suites to the test registry - CU_add_suite()
4)调用函数CU_add_test()将测试用例添加到测试包(suite)中。Add tests to the suites - CU_add_test() 
5)使用合适的接口来运行测试用例。Run tests using an appropriate interface, e.g. CU_console_run_tests
6)调用函数CU_cleanup_registry清除测试注册单元(Test Registry)。Cleanup the test registry - CU_cleanup_registry()



×××××××××××××××××××××××××××××××××××××××××××××××××××××以上为转载部分××××××××××××××××××××
付下面的代码
/*File:test.c *Auth:sjin *Date:2014-03-20 *Mail:413977243@qq.com */#include "test.h"#include <stdio.h>int sum(int a,int b){    return a + b;}
/*File:test.h *Auth:sjin *Date:2014-03-20 *Mail:413977243@qq.com */#ifndef __TEST_H_#define __TEST_H_int sum(int a,int b);#endif

/*File:testmain.c *Auth:sjin *Date:2014-03-20 *Mail:413977243@qq.com */#include "test.h"#include <CUnit/Console.h>#include <stdio.h>#include <assert.h>int InitSuite(){    return 0;}int EndSuite(){    return 0;}int Test_Is_Equal(int a,int b,int real){    int result;    result = sum(a,b);    if(result == real){        return 1;    }    return 0;}int Test_Is_Not_Equal(int a,int b,int real){    int result;    result = sum(a,b);    if(result != real){        return 1;    }    return 0;}void Test1(){    CU_ASSERT(Test_Is_Equal(3,4,7));}void Test2(){    CU_ASSERT(Test_Is_Not_Equal(4,5,10));}/*0:sucessful;1:failed */int AddTestMain(){#if 1    CU_pSuite pSuite = NULL;    /*****************     * 1.CU_add_suite 增加一个Suite     * 2.Suite名字:testSuite     * 3.InitSuite EndSuite 分别是测试单元初始和释放函数,如不需要传NULL     */    pSuite = CU_add_suite("testSuite",InitSuite,EndSuite);    //if(NULL != pSuite){        if(NULL == CU_add_test(pSuite,"Test1",Test1) ||            NULL == CU_add_test(pSuite,"Test2",Test2)){            return 1;        }   // }    /**********另一种测试方法**********/#else    CU_TestInfo testcases[] = {        {"Test1",Test1},        {"Test2",Test2},        CU_TEST_INFO_NULL    };    CU_SuiteIndo Suites[] = {        {"Test the functon sum:",InitSuite,EndSuite,testcases},        CU_TEST_INFO_NULL    };    if(CUE_SUCCESS != CU_register_suites(Suites)){        return 1;    }    /******************************/#endif    return 0;}
/*File:CuintRunTest.c *Auth:sjin *Date:2014-03-20 *Mail:413977243@qq.com */#include <stdio.h>#include <assert.h>#include <CUnit/Console.h>extern int AddTestMain();int main(){    //初始化    if(CUE_SUCCESS != CU_initialize_registry()){        return CU_get_error();    }    //返回注册到用例指针    assert(NULL != CU_get_registry);    //检测是否在执行    assert(!CU_is_test_running());    //调用那个测试模块完成测试用例    if(0 != AddTestMain()){        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();}

Makefile

INC=-I/usr/local/includeLIB=-L/usr/local/liball:test.c CUnitRunTest.c testMain.c        gcc $^ -o test $(INC) $(LIB) -lcunitclean:        rm -f test


编译过程中出现库加载不上时 
export LD_LIBRARY_PATH=/usr/local/lib


参靠资料:
1http://www.cnblogs.com/linux-sir/archive/2012/08/25/2654557.html
2'http://blog.csdn.net/colin719/article/details/1420583


1 0
原创粉丝点击