CPPUTest 单元测验框架(针对 C 单元测验的应用表达

2019-10-22 07:29 来源:未知

CPPUTest 固然名称上看起来是 C++ 的单元测验框架, 其实它也是帮忙测量试验 C 代码的.

本文首要介绍用CPPUTest来测验 C 代码. (C++没用过, 平常器重用的是C) C++相关的从头到尾的经过都简短了.

正文基于 debian v7.6 x86_64.

 

1. CPPUTest 安装

这段日子逐个Linux的发行版的源都有丰裕的软件财富, 并且安装方便.

不过只要想要在第不经常间使用新型版本的开源软件, 依然得从源码安装.

 

debian系统为了追求安定, apt源中的软件日常都比较旧. 所以本文中的例子是基于最新源码的CPPUTest.

 

1.1 apt-get 安装

$ sudo apt-get install cpputest

 

1.2 源码安装

1. 下载源码, 官网:

2. 编写翻译源码

$ tar zxvf cpputest-3.6.tar.gz
$ cd cpputest-3.6/
$ ./configure
$ make

 

最终自身从不实际安装, 而是间接运用编写翻译出的二进制。

 

2. CPPUTest 介绍

2.1 构造待测量试验代码 (C语言)

/* file: sample.h */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Student 
{
    char* name;
    int score;
};

void ret_void(void);
int ret_int(int, int);
double ret_double(double, double);
char* ret_pchar(char*, char*);

struct Student* init_student(struct Student* s, char* name, int score);

 

/* file: sample.c */

#include "sample.h"

#ifndef CPPUTEST
int main(int argc, char *argv[])
{
    char* pa;
    char* pb;
    pa = (char*) malloc(sizeof(char) * 80);
    pb = (char*) malloc(sizeof(char) * 20);

    strcpy(pa, "abcdefg");
    strcpy(pb, "hijklmn");

    printf ("Sample Start......n");

    ret_void();
    printf ("ret_int: %dn", ret_int(100, 10));
    printf ("ret_double: %.2fn", ret_double(100.0, 10.0));
    printf ("ret_pchar: %sn", ret_pchar(pa, pb));

    struct Student* s = (struct Student*) malloc(sizeof(struct Student));
    s->name = (char*) malloc(sizeof(char) * 80);

    init_student(s, "test cpputest", 100);
    printf ("init_Student: name=%s, score=%dn", s->name, s->score);
    printf ("Sample End  ......n");
    free(pa);
    free(pb);
    free(s->name);
    free(s);

    return 0;
}
#endif

void ret_void()
{
    printf ("Hello CPPUTest!n");
}

/* ia + ib */
int ret_int(int ia, int ib)
{
    return ia + ib;
}

/* da / db */
double ret_double(double da, double db)
{
    return da / db;
}

/* pa = pa + pb */
char* ret_pchar(char* pa, char* pb)
{
    return strcat(pa, pb);
}

/* s->name = name, s->score = score */
void init_student(struct Student* s, char* name, int score)
{
    strcpy(s->name, name);
    s->score = score;
}

 

2.2 测量试验用例的构成, 写法

CPPUTest 的测量检验用例特别轻易, 首先定义一个 TEST_GROUP, 然后定义属于这么些TEST_GROUP 的 TEST.

急需专一的地点是:

  1. 援用 CPPUTest 中的2个头文件

    #include #include

 

  1. 援引 C 头文件时, 供给动用 extern "C" {}

    extern "C" { #include "sample.h" }

 

下边的例子是测验 sample.c 中 ret_int 的代码.

结构了一个测量检验成功, 多少个测验战败的例子

/* file: test.c */

#include <CppUTest/CommandLineTestRunner.h>
#include <CppUTest/TestHarness.h>

extern "C"
{
#include "sample.h"
}


/* 定义个 TEST_GROUP, 名称为 sample */
TEST_GROUP(sample)
{};

/* 定义一个属于 TEST_GROUP 的 TEST, 名称为 ret_int_success */
TEST(sample, ret_int_success)
{
    int sum = ret_int(1, 2);
    CHECK_EQUAL(sum, 3);
}

/* 定义一个属于 TEST_GROUP 的 TEST, 名称为 ret_int_failed */
TEST(sample, ret_int_failed)
{
    int sum = ret_int(1, 2);
    CHECK_EQUAL(sum, 4);
}

int main(int argc, char *argv[])
{
    CommandLineTestRunner::RunAllTests(argc, argv);
    return 0;
}

 

2.3 测验用例结果剖断 ( fail, 种种assert等等)

测验完了后, 能够用 CPPUTest 提供的宏来推断测量检验结果是不是和预期后生可畏致.

CPPUTest 提供的用来判断的宏如下: (下边的测量试验代码就接受了 CHECK_EQUAL)

Assertion 宏

含义

CHECK(boolean condition) condition==True则成功; 反之失败
CHECK_TEXT(boolean condition, text) condition==True则成功; 反之失败, 并且失败时输出 text信息
CHECK_EQUAL(expected, actual) expected==actual则成功; 反之失败
CHECK_THROWS(expected_exception, expression) 抛出的异常 expected_exception==exception则成功; 反之失败
STRCMP_EQUAL(expected, actual) 字符串 expected==actual则成功; 反之失败
LONGS_EQUAL(expected, actual) 数字 expected==actual则成功; 反之失败
BYTES_EQUAL(expected, actual) 数字 expected==actual则成功; 反之失败 (数字是 8bit 宽)
POINTERS_EQUAL(expected, actual) 指针 expected==actual则成功; 反之失败
DOUBLES_EQUAL(expected, actual, tolerance) double型 expected和actual在误差范围内(tolerance)相等则成功; 反之失败
FAIL(text) 总是失败, 并输出 text 信息

 

2.4 运维测量检验用例时的编写翻译选项配置 (主若是C语言相关的)

这一步是最器重的, 也正是编写翻译出单元测量试验文件. 上边是 makefile 的写法, 关键地点加了注释.

# makefile for sample cpputest

CPPUTEST_HOME = /home/wangyubin/Downloads/cpputest-3.6

CC      := gcc
CFLAGS    := -g -Wall
CFLAGS  += -std=c99
CFLAGS  += -D CPPUTEST            # 编译测试文件时, 忽略sample.c的main函数, sample.c的代码中用了宏CPPUTEST

# CPPUTest 是C++写的, 所以用 g++ 来编译 测试文件
CPP     := g++
CPPFLAGS  := -g -Wall
CPPFLAGS  += -I$(CPPUTEST_HOME)/include

LDFLAGS := -L$(CPPUTEST_HOME)/lib -lCppUTest


sample: sample.o

sample.o: sample.h sample.c
    $(CC) -c -o sample.o sample.c $(CFLAGS)

# 追加的测试程序编译
test: test.o sample.o
    $(CPP) -o $@ test.o sample.o $(LDFLAGS)

test.o: sample.h test.c
    $(CPP) -c -o test.o test.c $(CPPFLAGS)


.PHONY: clean
clean:
    @echo "clean..."
    rm -f test sample
    rm -f sample.o test.o

 

编写翻译测量试验文件

make test  <-- 会生成一个文件名为 test 可执行文件

编写翻译sample程序时, 须要把 "CFLAGS  += -D CPPUTEST" 那句注释掉, 不然未有main函数.

 

2.5 运转测量试验用例, 查看结果的点子

运转可推行文件 test 就能够推行测验.

$ ./test    <-- 默认执行, 没有参数

test.c:34: error: Failure in TEST(sample, ret_int_failed)
    expected <3>
    but was  <4>
    difference starts at position 0 at: <          4         >
                                                   ^

..
Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 1 ms)

=================================================================================
$ ./test -c   <-- -c 执行结果加上颜色 (成功绿色, 失败红色)

test.c:34: error: Failure in TEST(sample, ret_int_failed)
    expected <3>
    but was  <4>
    difference starts at position 0 at: <          4         >
                                                   ^

..
Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 1 ms) <-- bash中显示红色

=================================================================================
$ ./test -v  <-- -v 显示更为详细的信息
TEST(sample, ret_int_failed)
test.c:34: error: Failure in TEST(sample, ret_int_failed)
    expected <3>
    but was  <4>
    difference starts at position 0 at: <          4         >
                                                   ^

 - 1 ms
TEST(sample, ret_int_success) - 0 ms

Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 1 ms)

=================================================================================
$ ./test -r 2   <-- -r 指定测试执行的次数, 这里把测试重复执行2遍
Test run 1 of 2

test.c:34: error: Failure in TEST(sample, ret_int_failed)
    expected <3>
    but was  <4>
    difference starts at position 0 at: <          4         >
                                                   ^

..
Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 0 ms)

Test run 2 of 2

test.c:34: error: Failure in TEST(sample, ret_int_failed)
    expected <3>
    but was  <4>
    difference starts at position 0 at: <          4         >
                                                   ^

..
Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 1 ms)

=================================================================================
$ ./test -g sample    <-- -g 指定 TEST_GROUP, 本例其实只有一个 TEST_GROUP sample

test.c:34: error: Failure in TEST(sample, ret_int_failed)
    expected <3>
    but was  <4>
    difference starts at position 0 at: <          4         >
                                                   ^

..
Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 1 ms)

=================================================================================
$ ./test -n ret_int_success    <-- -s 指定执行其中一个 TEST, 名称为 ret_int_success
.
OK (2 tests, 1 ran, 1 checks, 0 ignored, 1 filtered out, 0 ms)

=================================================================================
$ ./test -v -n ret_int_success  <-- 参数也可以搭配使用
TEST(sample, ret_int_success) - 0 ms

OK (2 tests, 1 ran, 1 checks, 0 ignored, 1 filtered out, 0 ms)

 

2.6 补充: setup and teardown

上面 test.c 文件中 TEST_GROUP(sample) 中的代码是空的, 其实 CPPUTest 中放置了 2 个调用 setup 和 teardown.

在 TEST_GROUP 中达成那2个函数之后, 每一种属于那个 TEST_GROUP 的 TEST 在实行从前都会调用 setup, 施行之后会调用 teardown.

修改 test.c 中的 TEST_GROUP 如下:

/* 定义个 TEST_GROUP, 名称为 sample */
TEST_GROUP(sample)
{
    void setup()
    {
        printf ("测试开始......n");
    }

    void teardown()
    {
        printf ("测试结束......n");
    }
};

 

双重施行测验: (每一个测验此前, 之后都多了地点的打字与印刷音讯)

$ make clean
clean...
rm -f test sample
rm -f sample.o test.o
$ make test
g++ -c -o test.o test.c -g -Wall -I/home/wangyubin/Downloads/cpputest-3.6/include
gcc -c -o sample.o sample.c -g -Wall -std=c99 -D CPPUTEST            
g++ -o test test.o sample.o -L/home/wangyubin/Downloads/cpputest-3.6/lib -lCppUTest
$ ./test -v
TEST(sample, ret_int_failed)测试开始......

test.c:44: error: Failure in TEST(sample, ret_int_failed)
    expected <3>
    but was  <4>
    difference starts at position 0 at: <          4         >
                                                   ^

测试结束......
 - 0 ms
TEST(sample, ret_int_success)测试开始......
测试结束......
 - 0 ms

Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 0 ms)

 

2.7 内部存款和储蓄器泄漏检查实验插件

内部存款和储蓄器泄漏一直是C/C++代码中令人头痛的主题材料, 幸好, CPPUTest 中提供了检查评定内部存款和储蓄器泄漏的插件, 使用那几个插件, 可使大家的代码越发健壮.

 

应用内部存款和储蓄器检查实验插件时, 测量试验代码待测代码 在编写翻译时都要引用.

-include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorMallocMacros.h

 

makefile 修改如下:

# makefile for sample cpputest

CPPUTEST_HOME = /home/wangyubin/Downloads/cpputest-3.6

CC      := gcc
CFLAGS    := -g -Wall
CFLAGS  += -std=c99
CFLAGS  += -D CPPUTEST            # 编译测试文件时, 忽略sample.c的main函数, sample.c的代码中用了宏CPPUTEST

# CPPUTest 是C++写的, 所以用 g++ 来编译 测试文件
CPP     := g++
CPPFLAGS  := -g -Wall
CPPFLAGS  += -I$(CPPUTEST_HOME)/include

LDFLAGS := -L$(CPPUTEST_HOME)/lib -lCppUTest

# 内存泄露检测
MEMFLAGS = -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorMallocMacros.h

sample: sample.o

sample.o: sample.h sample.c
    $(CC) -c -o sample.o sample.c $(CFLAGS) $(MEMFLAGS)

# 追加的测试程序编译
test: test.o sample.o
    $(CPP) -o $@ test.o sample.o $(LDFLAGS)

test.o: sample.h test.c
    $(CPP) -c -o test.o test.c $(CPPFLAGS)  $(MEMFLAGS)


.PHONY: clean
clean:
    @echo "clean..."
    rm -f test sample
    rm -f sample.o test.o

 

修改 sample.c 中的 init_student 函数, 构造二个内存泄漏的例子.

/* s->name = name, s->score = score */
void init_student(struct Student* s, char* name, int score)
{
    char* name2 = NULL;
    name2 = (char*) malloc(sizeof(char) * 80); /* 这里申请的内存, 最后没有释放 */
    strcpy(s->name, name2);

    strcpy(s->name, name);
    s->score = score;
}

 

修改 test.c 追加三个测验 init_student 函数的测验用例

TEST(sample, init_student)
{
    struct Student *stu = NULL;
    stu = (struct Student*) malloc(sizeof(struct Student));
    char name[80] = {'t', 'e', 's', 't', ''};

    init_student(stu, name, 100);
    free(stu);
}

 

实践测量检验, 能够开采测验结果中晋升 sample.c 72 行有内存泄漏危害,

那大器晚成行就是 init_student 函数中用 malloc 申请内存的那风度翩翩行.

$ make clean
clean...
rm -f test sample
rm -f sample.o test.o
$ make test
g++ -c -o test.o test.c -g -Wall -I/home/wangyubin/Downloads/cpputest-3.6/include  -include /home/wangyubin/Downloads/cpputest-3.6/include/CppUTest/MemoryLeakDetectorMallocMacros.h
gcc -c -o sample.o sample.c -g -Wall -std=c99 -D CPPUTEST             -include /home/wangyubin/Downloads/cpputest-3.6/include/CppUTest/MemoryLeakDetectorMallocMacros.h
g++ -o test test.o sample.o -L/home/wangyubin/Downloads/cpputest-3.6/lib -lCppUTest
$ ./test -v -n init_student
TEST(sample, init_student)测试开始......
测试结束......

test.c:47: error: Failure in TEST(sample, init_student)
    Memory leak(s) found.
Alloc num (4) Leak size: 80 Allocated at: sample.c and line: 72. Type: "malloc"
     Memory: <0x120c5f0> Content: ""
Total number of leaks:  1
NOTE:
    Memory leak reports about malloc and free can be caused by allocating using the cpputest version of malloc,
    but deallocate using the standard free.
    If this is the case, check whether your malloc/free replacements are working (#define malloc cpputest_malloc etc).


 - 0 ms

Errors (1 failures, 3 tests, 1 ran, 0 checks, 0 ignored, 2 filtered out, 0 ms)
TAG标签:
版权声明:本文由32450新蒲京网站发布于葡萄游戏厅_棋牌游戏,转载请注明出处:CPPUTest 单元测验框架(针对 C 单元测验的应用表达