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

标签:
cunitc语言单元测试测试用例代码结构框架测试模式it |
分类: deprecated |
原测试文件涉及文件读写操作,依赖文件 stdlib.h 库及 WinMain 等win32 API。
对环境配置要求较多,不适合于作为 CUnit 入门的实例。
编写 max() 和 min(),作为新的测试案例。
CUnit 测试文件框架不变,代码简化。
make文件,增加 ctags 命令。方便标签操作。(ctags 配置 使用)
typical of steps for using CUnit framework
(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()
2.初始化Test
3.把测试包(Test
4.加入测试用例(Test
5.使用适当的接口来运行测试测试程序,例如
6.清除Test
CUnit 结构框架
在CUnit的主页上可以看到对他结构简单描述
Test
CUnit的测试是单线程启动,只能注册一个测试用例Test
注册一个测试用例(如果已经注册了你可以cleanup掉然后重新注册使用)然后CU_add_suite增加你的模块然后CU_add_test再在你的模块下挂载你的模块内的测试函数。所有的挂载完毕后,调用你想使用的界面进行测试。
CUnit
测试模式
下面是四种测试模式:
1
2
3
4
测试实例
测试结果如下图:
共四个文件:
新浪博客发表时自动删掉了c语言的注释。或许是html代码的原因。
只有test_mathfunc.c的注释有用,可以参考 官网 http://cunit.sourceforge.net/example.html
http://s2/middle/6c07f2b6hb087a205ca91&690
test_mathfunc.c Tags open
------------ mathfunc.c ---------------------
int
max ( int x, int y )
{
}
int
min ( int x, int y )
{
}
---------- mathfunc.h
#ifndef
#define
int max( int x, int y);
int min( int x, int y);
#endif
------------- test_mathfunction.c ----------------
#include <stdio.h>
#include <string.h>
#include "CUnit/Basic.h"
#include
int init_suite1(void)
{
}
int clean_suite1(void)
{
}
void testMax(void)
{
}
void testMin(void)
{
}
int main()
{
}
---------------- make ------------------------------
.PHONY: clean ctags
test_mathfunc: test_mathfunc.o mathfunc.o
test_mathfunc.o: test_mathfunc.c mathfunc.h
mathfunc.o: mathfunc.c mathfunc.h
clean:
ctags: