当前位置:首页 > 行业动态 > 正文

如何利用 CppuTest 在 Linux 环境下进行单元测试?

Cpputest 是一个用于 C/C++ 的单元测试框架,它支持 Linux 操作系统上的测试用例编写和执行。

CppUTest是一款灵活且功能强大的单元测试框架,适用于C和C++项目,它不仅支持简单的单元测试,还能进行复杂的测试用例设计和执行,本文将详细介绍如何在Linux系统上安装和使用CppUTest,并提供一些示例代码来帮助用户更好地理解和应用这个工具。

安装CppUTest

1. 使用apt-get安装(适用于Debian系发行版)

sudo apt-get install cpputest

这种方法简单快捷,但可能无法获取到最新版本。

2. 从源码安装

如果需要最新功能或特定版本,可以从源码安装CppUTest,以下是详细步骤:

1、下载源码

 wget http://cpputest.github.io/downloads/cpputest-3.6.tar.gz

2、解压并编译

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

3. 配置环境变量

如何利用 CppuTest 在 Linux 环境下进行单元测试?

为了方便使用,可以配置环境变量:

export C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/local/cpputest/include  
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/local/cpputest/include
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/cpputest/lib  
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cpputest/lib

编写测试用例

CppUTest的测试用例由TEST_GROUP和TEST组成,下面是一个基本的示例:

1. 待测试代码(sample.h和sample.c)

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);

sample.c

#include "sample.h"
#ifndef CPPUTEST
int main(int argc, char *argv[]) {
    char* pa = (char*) malloc(sizeof(char) * 80);
    char* pb = (char*) malloc(sizeof(char) * 20);
    strcpy(pa, "abcdefg");
    strcpy(pb, "hijklmn");
    printf("Sample Start......
");
    ret_void();
    printf("ret_int: %d
", ret_int(100, 10));
    printf("ret_double: %.2f
", ret_double(100.0, 10.0));
    printf("ret_pchar: %s
", 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=%d
", s->name, s->score);
    printf("Sample End ......
");
    free(pa);
    free(pb);
    free(s->name);
    free(s);
    return 0;
}
#endif
void ret_void() {
    printf("Hello CPPUTest!
");
}
int ret_int(int ia, int ib) {
    return ia + ib;
}
double ret_double(double da, double db) {
    return da / db;
}
char* ret_pchar(char* pa, char* pb) {
    return strcat(pa, pb);
}
struct Student* init_student(struct Student* s, char* name, int score) {
    strcpy(s->name, name);
    s->score = score;
    return s;
}

2. 测试用例代码(test.c)

如何利用 CppuTest 在 Linux 环境下进行单元测试?

#include <CppUTest/CommandLineTestRunner.h>
#include <CppUTest/TestHarness.h>
extern "C" {
#include "sample.h"
}
TEST_GROUP(sample) {
};
TEST(sample, ret_int_success) {
    int sum = ret_int(1, 2);
    CHECK_EQUAL(sum, 3);
}
TEST(sample, ret_int_failed) {
    int sum = ret_int(1, 2);
    CHECK_EQUAL(sum, 4); // 这个测试会失败
}
int main(int argc, char *argv[]) {
    return CommandLineTestRunner::RunAllTests(argc, argv);
}

运行测试用例

编译并运行测试用例:

g++ -o testcpp test.cpp -lCppUTest -lCppUTestExt
./testcpp

输出结果将显示测试通过与否的信息。

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

表示有一个测试未通过。

常见问题解答(FAQs)

Q1:如何确保CppUTest已正确安装?

A1:可以通过编写一个简单的测试用例来验证,创建一个名为simple_test.cpp的文件,内容如下:

#include <CppUTest/CommandLineTestRunner.h>
#include <CppUTest/TestHarness.h>
TEST_GROUP(SimpleTestGroup) {};
TEST(SimpleTestGroup, SimpleTest) { CHECK(true); }
int main(int argc, char *argv[]) { return CommandLineTestRunner::RunAllTests(argc, argv); }

然后编译并运行:

如何利用 CppuTest 在 Linux 环境下进行单元测试?

g++ simple_test.cpp -o simple_test -lCppUTest -lCppUTestExt
./simple_test

如果输出类似以下内容,则说明CppUTest已正确安装:

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

Q2:如何处理测试用例中的资源清理?

A2:在CppUTest中,可以使用teardown()方法进行资源清理。

TEST_GROUP(ResourceTestGroup) {
    void teardown() { /* 清理代码 */ }
};
TEST(ResourceTestGroup, TestWithResourceCleanup) { /* 测试代码 */ }

在每个测试用例运行结束后,teardown()方法会自动被调用,确保资源得到释放。

小编有话说

CppUTest作为一个轻量级但功能强大的单元测试框架,非常适合用于各种规模的C和C++项目,通过本文的介绍,希望读者能够掌握在Linux环境下安装和使用CppUTest的基本方法,并能在实际项目中灵活应用,单元测试不仅能提高代码质量,还能为项目的长期维护打下坚实的基础。