使用CUnit对库做单元测试

15 篇文章 0 订阅

前言:

博主目前手头上管理着几个设备的跨平台库,对外接口大概有七八十个,平常写测试用例的test文件就有几十个。趁着最近版本发布后的空闲时间,对接口库进行单元测试。与CppUnit类似,CUnit为C程序员提供了基本的测试功能。

安装:

博主根据源码内部工程进行winodw平台库编译时,遇到各种问题,修改了少许源码后,方能正常运行,已将库文件上传

Cunit源码的下载地址是:https://sourceforge.net/projects/cunit/postdownload

Windows库文件下载地址: https://download.csdn.net/download/wjb123sw99/12311601

使用文档:http://cunit.sourceforge.net/doc/index.html

使用:

使用CUnit官网所提供的Demo,让我们快速熟悉CUnit整体操作。

#include <stdio.h>
#include <string.h>
#include "CUnit/Basic.h"
#include "CUnit/Automated.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((char *)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_list_tests_to_file();
   CU_automated_run_tests();
   CU_cleanup_registry();
   return CU_get_error();
}


上述程序运行完后,生成CUnitAutomated-Listing.xml、CUnitAutomated-Results.xml文件,将CUnit-List.dtd、CUnit-List.xsl、CUnit-Run.dtd、CUnit-Run.xsl(这几个文件在CUnit的源码包可以找到)和XML文件放到同一级目录,使用window自带的IE浏览器打开,即可看到单元测试结果。

下面讲解下Demo中所使用到的CUnit函数。

函数:

CU_ErrorCode CU_initialize_registry(void);

用户使用CUnit前,必须运行CU_initialize_registry接口进行测试框架初始化。

错误码:

CUE_SUCCESS初始化成功。
CUE_NOMEMORY内存分配失败。

void  CU_cleanup_registry(void);

 用户使用CUnit后,必须运行CU_cleanup_registry接口用于释放测试框架

CU_pSuite CU_add_suite(const char* strName, CU_InitializeFunc pInit, CU_CleanupFunc pClean);

CU_add_suite函数用于用户向测试框架注册一个单元测试;strName【入参】:单元测试的名称,必须在框架内唯一;pInit【入参】:单元测试初始化程序,类似于构造函数;pClean【入参】:单元测试结束程序,类似于析构函数。CU_InitializeFunc 函数格式定义如下:

typedef int  (*CU_InitializeFunc)(void);  /**< Signature for suite initialization function. */

如果该注册函数不需要pInit或者pClean函数,则可以设为NULL。该函数成功返回单元测试指针CU_pSuite,失败则返回NULL。

CU_pTest  CU_add_test(CU_pSuite pSuite, const char* strName, CU_TestFunc pTestFunc);

CU_add_test函数用于用户向单元测试中增加一个测试用例,pSuite【入参】:单元测试指针。strName【入参】:测试用例名称;

pTestFunc【入参】:测试用例函数。CU_TestFunc 函数格式定义如下:

typedef void (*CU_TestFunc)(void);        /**< Signature for a testing function in a test case. */

CU_add_test函数成功则返回测试用例指针,失败则返回NULL。

void  CU_basic_set_mode(CU_BasicRunMode mode);

设置基本运行模式,该模式在测试运行期间控制输出,mode【入参】:可选择下列参数值

CU_BRM_NORMAL打印失败和运行摘要。
CU_BRM_SILENT除错误消息外,不输出任何输出。
CU_BRM_VERBOSE运行详细信息的最大输出。

CU_ErrorCode CU_list_tests_to_file(void);

如果用户想使用Automated模式输出XML报表,则调用该函数。

void  CU_automated_run_tests(void);

使用Automated模式运行测试用例

CU_ErrorCode   CU_get_error(void);

由于CUnit部分函数错误时,返回NULL。如果用户想获取具体错误码,则需要调用CU_get_error函数。

const char*    CU_get_error_msg(void);

由于CUnit部分函数错误时,返回NULL。如果用户想获取具体错误信息,则需要调用CU_get_error_msg函数。

断言:

CUnit提供了一组用于测试逻辑条件的断言。这些断言的成功或失败由框架跟踪,可以在测试运行完成时查看。

CU_ASSERT(int expression)
CU_ASSERT_FATAL(int expression)
CU_TEST(int expression)
CU_TEST_FATAL(int expression)

Assert that expression is TRUE (non-zero)

CU_ASSERT_TRUE(value)
CU_ASSERT_TRUE_FATAL(value)

Assert that value is TRUE (non-zero)

CU_ASSERT_FALSE(value)
CU_ASSERT_FALSE_FATAL(value)

Assert that value is FALSE (zero)

CU_ASSERT_EQUAL(actual, expected)
CU_ASSERT_EQUAL_FATAL(actual, expected)

Assert that actual = = expected

CU_ASSERT_NOT_EQUAL(actual, expected))
CU_ASSERT_NOT_EQUAL_FATAL(actual, expected)

Assert that actual != expected

CU_ASSERT_PTR_EQUAL(actual, expected)
CU_ASSERT_PTR_EQUAL_FATAL(actual, expected)

Assert that pointers actual = = expected

CU_ASSERT_PTR_NOT_EQUAL(actual, expected)
CU_ASSERT_PTR_NOT_EQUAL_FATAL(actual, expected)

Assert that pointers actual != expected

CU_ASSERT_PTR_NULL(value)
CU_ASSERT_PTR_NULL_FATAL(value)

Assert that pointer value == NULL

CU_ASSERT_PTR_NOT_NULL(value)
CU_ASSERT_PTR_NOT_NULL_FATAL(value)

Assert that pointer value != NULL

CU_ASSERT_STRING_EQUAL(actual, expected)
CU_ASSERT_STRING_EQUAL_FATAL(actual, expected)

Assert that strings actual and expected are equivalent

CU_ASSERT_STRING_NOT_EQUAL(actual, expected)
CU_ASSERT_STRING_NOT_EQUAL_FATAL(actual, expected)

Assert that strings actual and expected differ

CU_ASSERT_NSTRING_EQUAL(actual, expected, count)
CU_ASSERT_NSTRING_EQUAL_FATAL(actual, expected, count)

Assert that 1st count chars of actual andexpected are the same

CU_ASSERT_NSTRING_NOT_EQUAL(actual, expected, count)
CU_ASSERT_NSTRING_NOT_EQUAL_FATAL(actual, expected, count)

Assert that 1st count chars of actual andexpected differ

CU_ASSERT_DOUBLE_EQUAL(actual, expected, granularity)
CU_ASSERT_DOUBLE_EQUAL_FATAL(actual, expected, granularity)

Assert that |actual - expected| <= |granularity|
Math library must be linked in for this assertion.

CU_ASSERT_DOUBLE_NOT_EQUAL(actual, expected, granularity)
CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL(actual, expected, granularity)

Assert that |actual - expected| > |granularity|
Math library must be linked in for this assertion.

CU_PASS(message)

Register a passing assertion with the specified message. No logical test is performed.

CU_FAIL(message)
CU_FAIL_FATAL(message)

Register a failed assertion with the specified message. No logical test is performed.

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: CUnit是一个用于开发和执行C程序的单元测试框架。开发者可以使用CUnit来编写测试用例,以验证程序的各个模块的功能是否正确。以下是CUnit单元测试用例开发的一般流程: 1. 安装CUnit:首先需要从CUnit的官方网站下载并安装CUnit。 2. 引入头文件:在测试用例的C文件中引入CUnit的头文件,以便使用CUnit的相关函数和宏。 3. 定义测试用例:在测试用例的C文件中,可以使用CUnit提供的宏来定义测试用例及其相关的测试函数。测试函数应该包含一系列测试断言,用于验证被测程序的输出是否符合预期。 4. 初始化测试套件和测试注册:使用CUnit提供的宏和函数,初始化测试套件并将测试用例注册到测试套件中。 5. 执行测试用例:使用CUnit提供的函数,执行测试套件中的所有测试用例。 6. 生成测试报告:CUnit会自动记录测试结果,包括测试通过和测试失败的情况,还可以生成详细的测试报告。 7. 分析和修复错误:根据测试报告,开发者可以分析测试失败的原因,并修改被测程序中的错误。 通过CUnit单元测试用例开发,可以有效地提高程序的质量和稳定性。测试用例可以覆盖程序的各个功能模块,验证其正确性和健壮性。同时,CUnit还能提供详细的测试报告,让开发者更容易发现并修复错误。 ### 回答2: CUnit是一个用于C语言项目的单元测试框架。在软件开发过程中,为了保证代码的质量和稳定性,需要对不同的函数模块进行单元测试CUnit可以帮助开发人员编写和执行这些测试用例。 用CUnit进行单元测试用例开发需要以下几个步骤: 第一步是创建测试用例。测试用例是一段测试代码,用于验证功能模块的正确性。开发人员需要根据功能要求和预期结果,编写一系列测试用例。 第二步是编写测试代码。测试代码中包含了一系列宏和函数,用于定义测试集合、测试套件和测试用例。开发人员需要定义不同的测试集合,并将测试用例添加到相应的集合中。 第三步是执行测试。通过调用CUnit提供的函数,开发人员可以执行之前定义的测试集合。CUnit将自动执行测试用例,并记录测试结果。开发人员可以查看测试结果,以确定功能模块的正确性。 第四步是分析测试结果。通过查看测试结果,开发人员可以了解哪些测试用例通过了,哪些失败了。通过分析失败的测试用例,可以找到代码中的问题,并进行修复。 最后一步是反复迭代测试过程。在软件开发过程中,需要不断进行单元测试,以确保代码的质量和稳定性。开发人员可以修改测试用例和测试代码,并重复执行测试过程,直到代码满足预期结果为止。 通过CUnit单元测试用例开发,开发人员可以更好地验证和调试功能模块,提高代码的质量和稳定性,从而提高整个软件项目的可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值