第五章 UT单元测试——CUnit框架学习

系列文章目录

第一章 UT单元测试——GoogleTest通用构建说明
第二章 UT单元测试——GTest框架实例
第三章 UT单元测试——CPU与内存使用率限制
第四章 UT单元测试——gcov/lcov代码覆盖率测试
第五章 UT单元测试——CUnit框架学习



前言

CUnit官方地址:http://cunit.sourceforge.net/index.html


一、CUnit是什么?

CUnit是一套用于编写、管理、运行C代码的轻量级单元测试系统,它向C程序员提供基础测试功能,并且具有多种灵活的用户接口。

CUnit可编译成静态库链接至用户的测试代码,它采用简单框架构建测试架构,并且提供丰富的断言集用来测试常用数据类型。此外,它还提供几种不同的接口用来运行测试和报告结果。当前这些接口包括:

Automated输出xml文件非交互式
Basic灵活的编程接口非交互式
Console控制台接口(ansi C)交互式
Curses图形化接口(Unix)交互式

二、CUnit环境配置

1.Ubuntu安装CUnit

这里介绍Ubuntu20.04环境下如何安装CUnit。

sudo apt-get install libcunit1-dev
# 之前按其他教程从源码安装,因此机器里安装过libcunit1 automake libtool这些包, 
# 不确定是否存在依赖,有时间我会测试下。

如果想从源码安装,可以去官方下载。由于安装依赖及编译步骤繁琐,笔者放弃了该方法。

二、使用方法

1.编写测试用例

本文使用官方示例代码

/*
 *  Simple example of a CUnit unit test.
 *
 *  This program (crudely) demonstrates a very simple "black box"
 *  test of the standard library functions fprintf() and fread().
 *  It uses suite initialization and cleanup functions to open
 *  and close a common temporary file used by the test functions.
 *  The test functions then write to and read from the temporary
 *  file in the course of testing the library functions.
 *
 *  The 2 test functions are added to a single CUnit suite, and
 *  then run using the CUnit Basic interface.  The output of the
 *  program (on CUnit version 2.0-2) is:
 *
 *           CUnit : A Unit testing framework for C.
 *           http://cunit.sourceforge.net/
 *
 *       Suite: Suite_1
 *         Test: test of fprintf() ... passed
 *         Test: test of fread() ... passed
 *
 *       --Run Summary: Type      Total     Ran  Passed  Failed
 *                      suites        1       1     n/a       0
 *                      tests         2       2       2       0
 *                      asserts       5       5       5       0
 */

#include <stdio.h>
#include <string.h>
#include "CUnit/Basic.h"

/* Pointer to the file used by the tests. */
static FILE* temp_file = NULL;

/* The suite initialization function.
 * Opens the temporary file used by the tests.
 * Returns zero on success, non-zero otherwise.
 */
int init_suite1(void)
{
   if (NULL == (temp_file = fopen("temp.txt", "w+"))) {
      return -1;
   }
   else {
      return 0;
   }
}

/* The suite cleanup function.
 * Closes the temporary file used by the tests.
 * Returns zero on success, non-zero otherwise.
 */
int clean_suite1(void)
{
   if (0 != fclose(temp_file)) {
      return -1;
   }
   else {
      temp_file = NULL;
      return 0;
   }
}

/* Simple test of fprintf().
 * Writes test data to the temporary file and checks
 * whether the expected number of bytes were written.
 */
void testFPRINTF(void)
{
   int i1 = 10;

   if (NULL != temp_file) {
      CU_ASSERT(0 == fprintf(temp_file, ""));
      CU_ASSERT(2 == fprintf(temp_file, "Q\n"));
      CU_ASSERT(7 == fprintf(temp_file, "i1 = %d", i1));
   }
}

/* Simple test of fread().
 * Reads the data previously written by testFPRINTF()
 * and checks whether the expected characters are present.
 * Must be run after testFPRINTF().
 */
void testFREAD(void)
{
   unsigned char buffer[20];

   if (NULL != temp_file) {
      rewind(temp_file);
      CU_ASSERT(9 == fread(buffer, sizeof(unsigned char), 20, temp_file));
      CU_ASSERT(0 == strncmp(buffer, "Q\ni1 = 10", 9));
   }
}

/* The main() function for setting up and running the tests.
 * Returns a CUE_SUCCESS on successful running, another
 * CUnit error code on failure.
 */
int main()
{
   CU_pSuite pSuite = NULL;

   /* initialize the CUnit test registry */
   if (CUE_SUCCESS != CU_initialize_registry())
      return CU_get_error();

   /* add a suite to the registry */
   pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
   if (NULL == pSuite) {
      CU_cleanup_registry();
      return CU_get_error();
   }

   /* add the tests to the suite */
   /* NOTE - ORDER IS IMPORTANT - MUST TEST fread() AFTER fprintf() */
   if ((NULL == CU_add_test(pSuite, "test of fprintf()", testFPRINTF)) ||
       (NULL == CU_add_test(pSuite, "test of fread()", testFREAD)))
   {
      CU_cleanup_registry();
      return CU_get_error();
   }

   /* Run all tests using the CUnit Basic interface */
   CU_basic_set_mode(CU_BRM_VERBOSE);
   CU_basic_run_tests();
   CU_cleanup_registry();
   return CU_get_error();
}

2.进行测试

步骤如下:

# 编译,参数说明:链接CUnit库,并生成代码覆盖率测试报告
gcc -fprofile-arcs -ftest-coverage -o example example.c -lcunit

# 运行
./example

# 生成覆盖率中间数据文件
gcov example.c

# 生成xml形式的覆盖率报告文件至app.info文件
lcov --directory . --capture --output-file app.info

# 生成html形式的覆盖率报告文件至results文件夹,数据来源文件为app.info
genhtml -o results app.info

正常的话会生成html页面。

3.测试结果

CUnit测试结果:
CUnit运行结果

lcov测试结果1:

lcov运行结果1

lcov测试结果2:

lcov运行结果2


总结

以上就是今天要讲的内容,本文仅仅简单介绍了CUnit框架的使用,而CUnit框架提供了大量能使我们快速便捷测试的方法。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
黑盒测试是一种测试方法,通过不了解程序内部的实现细节和代码的情况下,仅通过输入和输出来验证程序的正确性。CUnit是一种用于C语言的单元测试框架,用于对C语言程序进行黑盒测试。 在黑盒测试CUnit框架实验中,首先需要编写测试用例。测试用例是根据程序的需求和功能编写的一组输入和预期输出的组合,用于验证程序的正确性。每个测试用例通常包括输入数据、预期输出和实际输出。 接下来,可以使用CUnit框架来组织和执行测试用例。CUnit提供了一系列的库函数和宏,用于创建测试套件、测试用例和断言。测试套件是一组相关的测试用例的集合,用于组织和管理测试用例的执行。测试用例是对程序的具体功能进行测试的实例,用于验证程序的正确性。断言是用于检查程序的实际输出与预期输出是否一致的工具,如果断言失败,则表示程序存在问题。 在执行测试用例时,CUnit会自动运行每个测试用例,并将实际输出与预期输出进行比较。如果实际输出与预期输出一致,则测试通过;如果实际输出与预期输出不一致,则测试失败。测试结果会被记录并汇总,包括测试通过的用例数量、测试失败的用例数量和测试通过率等信息。 通过黑盒测试CUnit框架实验,可以有效地验证程序的正确性,提高程序的质量和稳定性。同时,CUnit框架还提供了灵活的测试组织和管理机制,方便对程序进行持续集成和自动化测试,提高开发效率和测试效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喜乐boy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值