Back to the C Programming Language

CUnit

1. Overview

1.1. 单元测试

单元测试(Unit Testing)又被称作模块测试,是针对程序模块(软件设计的最⼩单位)来进⾏正确性检验的测试工作。

单元测试是软件开发中的重要一环,可以穿插在编码前后及debug环节中;前者重点设计验证关键功能,后者侧重验证边界条件。

测试部件的选择与构建是完成一个优雅单元测试的前提。如果需要验证抽象度比较高的功能,往往需要首先构建较为复杂的测试容器封装业务上下文及与业务无关的底层操作。

单元测试带来的好处:

1.2. CUnit

     CUnit is a lightweight system for writing, administering, and running unit tests in C.  It provides C programmers a basic testing functionality with a flexible variety of user interfaces.

     CUnit is built as a static library which is linked with the user's testing code.  It uses a simple framework for building test structures, and provides a rich set of assertions for testing common data types.   In addition, several different interfaces are provided for running tests and reporting results. 

CUnit是一个在C语言环境中用来编写、管理和运行单元测试的轻量系统。它为C程序员提供了基于丰富用户接口(a flexible variety of user interfaces)的基础测试功能。

CUnit用静态库(built as a static library)来连接(link)用户测试代码。它用一个简单的框架来建立测试结构,为常用数据类型的测试提供了丰富的断言语句,并为运行测试和报告测试结果提供各种接口,包括通过代码自动运行测试并报告测试结果的接口,还有使用户可以动态运行测试并查看结果的交互式接口。

CUnit由C语言写成,具备很好的平台无关性,支持Windows, Linux, Mac OS X。

2. Getting Start

2.1. Install

# Mac OS brew install cunit --build-from-source

# Linux

#wget http://jaist.dl.sourceforge.net/project/cunit/CUnit/2.1-2/CUnit-2.1-2-src.tar.bz2
wget http://jaist.dl.sourceforge.net/project/cunit/CUnit/2.1-3/CUnit-2.1-3.tar.bz2
tar xjf CUnit-2.1-3-src.tar.bz2
cd CUnit-2.1-3
./configure --enable-debug=yes --enable-curses=yes --enable-examples=yes
make
make install

2.2. First Case

cunit_sample.c: Refer to http://cunit.sourceforge.net/example.html

CUnit Sample on Github

2.3. Compile

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
gcc -lcunit cunit_sample.c -o suite

2.4. Run

$ ./suite 


     CUnit - A unit testing framework for C - Version 2.1-2
     http://cunit.sourceforge.net/


Suite: Suite_1
  Test: test of fprintf() ...passed
  Test: test of fread() ...passed

Run Summary:    Type  Total    Ran Passed Failed Inactive
              suites      1      1    n/a      0        0
               tests      2      2      2      0        0
             asserts      5      5      5      0      n/a

Elapsed time =    0.000 seconds

3. Programmers Guide

3.1. CUnit测试例组织结构

                      Test Registry
                            |
             ------------------------------
             |                            |
          Suite '1'      . . . .       Suite 'N'
             |                            |
       ---------------             ---------------
       |             |             |             |
    Test '11' ... Test '1M'     Test 'N1' ... Test 'NM'

3.2. 编写CUnit的主要步骤

  1. Write functions for tests (and suite init/cleanup if necessary).
  2. Initialize the test registry - CU_initialize_registry()
  3. Add suites to the test registry - CU_add_suite()
  4. Add tests to the suites - CU_add_test()
  5. Run tests using an appropriate interface, e.g. CU_console_run_tests
  6. Cleanup the test registry - CU_cleanup_registry

3.2.1. CUnit断言(Assertions)

The assertions defined by CUnit are:#include <CUnit/CUnit.h>

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 and expected 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 and expected 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.

3.2.2. CUnit的运行模式

If these interfaces are defined in <CUnit/TestRun.h>.

Interface

Platform

Description

Parameters

Automated

all

常用方式,输出测试详情到xml文件

CU_set_output_filename(Base_Dir);
CU_list_tests_to_file(); //Base_Dir-Listing.xml
CU_automated_run_tests(); //Base_Dir-Results.mxl

Basic

all

常用方式,输出测试概要到标准输出

CU_basic_set_mode(CU_BRM_SILENT);

Console

all

interactive console mode under user control

Curses

Linux/Unix

interactive curses mode under user control

4. Reference


CategoryDev

MainWiki: CUnit (last edited 2013-05-08 00:11:07 by twotwo)