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

本文介绍了CUnit在C语言中的单元测试用法,通过编写max()和min()函数作为测试案例,详细阐述了CUnit的结构框架、测试模式,并提供了测试实例。文章展示了如何注册和添加测试用例,以及测试流程。测试结果显示测试成功通过。
摘要由CSDN通过智能技术生成

修改官网测试 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书写代测试的函数(如果必要,需要写suiteinit/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'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值