第五章 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框架提供了大量能使我们快速便捷测试的方法。

猜你喜欢

转载自blog.csdn.net/zmkzmkok/article/details/115012238