设为首页 加入收藏

TOP

C语言单元测试(一)
2014-04-06 17:41:48 来源: 作者: 【 】 浏览:280
Tags:语言 单元 测试

  关于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;

  }

  </STDIO.H>

  /*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

  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;

  }

  // }

   

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言正则表达式的使用 下一篇C语言语句及流程控制

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: