c语言 自动测试,CUnit(c语言的单元测试)用法实例

修改官网测试 fprintf() 和 fread() 的 test.c 而得。

原测试文件涉及文件读写操作,依赖文件 stdlib.h 库及 WinMain 等win32 API。

对环境配置要求较多,不适合于作为 CUnit 入门的实例。

编写 max() 和 min(),作为新的测试案例。

CUnit 测试文件框架不变,代码简化。

make文件,增加 ctags 命令。方便标签操作。(ctags 配置 使用)

typical of steps for using CUnit

framework——对 test_mathfunc.c 文件的解释,.

(1) Write functions for tests (and suite init/cleanup if

necessary)

(2) Initialize the test registry, by calling

CU_initialize_registry()

(3) Add suites to the test registry, by calling

CU_add_suite()

(4) Add tests to the suites, by calling CU_add_test()

(5) Run tests using an appropriate interface, e.g. by calling

CU_console_run_tests()

(6) Cleanup the test registry, by calling CU_cleanup_registry()

1.书写代测试的函数(如果必要,需要写suite的init/cleanup函数)

2.初始化Test Registry - CU_initialize_registry()

3.把测试包(Test Suites)加入到Test Registry - CU_add_suite()

4.加入测试用例(Test Case)到测试包当中- CU_add_test()

5.使用适当的接口来运行测试测试程序,例如 CU_console_run_tests()

6.清除Test Registry - CU_cleanup_registry()

CUnit 结构框架

在CUnit的主页上可以看到对他结构简单描述

Test Registry|------------------------------| |Suite '1' . . . . Suite 'N'| |--------------- ---------------| | | |Test '11' ... Test '1M' Test 'N1' ... Test 'NM'

CUnit的测试是单线程启动,只能注册一个测试用例TestRegistry, 一次测试(Test Registry)可以运行多个测试包(Test Suite),而每个测试包可以包括多个测试用例(TestCase),每个测试用例又包含一个或者多个断言类的语句。具体到程序的结构上,一次测试下辖多个TestSuite,它对应于程序中各个独立模块;一个Suite管理多个TestCase,它对应于模块内部函数实现。每个Suite可以含有setup和teardown函数,分别在执行suite的前后调用。

注册一个测试用例(如果已经注册了你可以cleanup掉然后重新注册使用)然后CU_add_suite增加你的模块然后CU_add_test再在你的模块下挂载你的模块内的测试函数。所有的挂载完毕后,调用你想使用的界面进行测试。

CUnit 测试模式

下面是四种测试模式:1Automated Output to xml file Non-interactive2Basic Flexible programming interface Non-interactive3Console Console interface (ansi C) Interactive4Curses Graphical interface (Unix) Interact

测试实例

测试结果如下图:

Test: test of Min() ...passed

Test: test of Min() ...passed

表示测试成功通过。

a4c26d1e5885305701be709a3d33442f.png

共四个文件:

make mathfunc.h mathfunc.c

test_mathfunc.c

新浪博客发表时自动删掉了c语言的注释。或许是html代码的原因。

只有test_mathfunc.c的注释有用,可以参考 官网 http://cunit.sourceforge.net/example.html

a4c26d1e5885305701be709a3d33442f.png

test_mathfunc.c Tags open

------------ mathfunc.c ---------------------

int  max ( int x, int y )

{

return x

> y? x : y;

} int

min ( int x, int y )

{

return x

< y? x :

y; }

---------- mathfunc.h ----------------

#ifndef _MATHFUNC_H_

#define _MATHFUNC_H_

int max( int x, int y);

int min( int x, int y);

#endif ------------- test_mathfunction.c ----------------

#include

#include

#include "CUnit/Basic.h"

#include "mathfunc.h"

int init_suite1(void)

{

return

0;

}

int clean_suite1(void)

{

return

0;

}

void testMax(void)

{

CU_ASSERT(3

== max(2,3) );

}

void testMin(void)

{

CU_ASSERT(2

== 2);

}

int main()

{

CU_pSuite

pSuite = NULL;

if

(CUE_SUCCESS != CU_initialize_registry())

return CU_get_error();

pSuite =

CU_add_suite("Suite_1", init_suite1, clean_suite1);

if (NULL ==

pSuite)

{

CU_cleanup_registry();

return CU_get_error();

}

if ((NULL ==

CU_add_test(pSuite, "test of Max()", testMax)) ||

(NULL == CU_add_test(pSuite, "test of Min()", testMin)))

{

CU_cleanup_registry();

return CU_get_error();

}

CU_basic_set_mode(CU_BRM_VERBOSE);

CU_basic_run_tests();

CU_cleanup_registry();

return

CU_get_error();

}

---------------- make ------------------------------

.PHONY: clean ctags

test_mathfunc: test_mathfunc.o mathfunc.o

gcc -o

test_mathfunc test_mathfunc.o mathfunc.o -lcunit

test_mathfunc.o: test_mathfunc.c mathfunc.h

gcc -c

test_mathfunc.c

mathfunc.o: mathfunc.c mathfunc.h

gcc -c

mathfunc.c

clean:

rm -f

test_mathfunc test_mathfunc.exe *.o

ctags:

ctags -R

--c-kinds=+p --fields=+S

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值