VC++6.0 配置CppUTest测试环境

  最近看《软件项目成功之道》,书中无数次提及到“单元测试”对于项目成败的重要性,看到同事将CppUTest用于Linux动态库测试,于是在VC++6.0环境下搭建一个基于CppUTest的单元测试环境,用于测试工作中编写的C函数。下面就来介绍整个搭建过程,整个过程分为四步:前期准备——需要的库;环境配置——VC++6.0配置;实例演示——可复现的Demo;拓展——后续学习资料。

  一、前期准备;

  • CppUTest下载
    • 官网:http://cpputest.github.io/
    • 历史版本:https://github.com/cpputest/cpputest/releases

  在我的项目中,采用的是老版本V3.3;

  二、环境配置;

  1、利用VC++6.0,新建一个用于测试的工程CppUTestLearning;

  2、选择Project——>Settings,配置Code Generation,选择“Debug Multithreaded DLL”;

  VC++6.0 配置CppUTest测试环境

  3、配置链接库,其中CppUTest.lib是测试框架的核心,winmm.lib用于解决问题“error LNK2001: unresolved external symbol __imp__timeGetTime”;

  VC++6.0 配置CppUTest测试环境

  4、编译时,需要忽略“libcmtd.lib”这个库;Project——>Setting——>Link,选择Category->Input,在Ignore libraries中填入libcmtd;

  VC++6.0 配置CppUTest测试环境

  5、设置CppUTest的公共头文件存放路径,选择Tools——>Options——Directories,添加\CPPUTEST和\PLATFORMS\VISUALCPP两个目录;

  VC++6.0 配置CppUTest测试环境

  三、实例演示

  1、编写用于测试的开发程序MyProgram.h和MyProgram.c

MyProgram.h

#ifndef MY_PROGRAM_H
#define MY_PROGRAM_H
/*
* Description: 整数加法函数
* Function: add
* Parameter: first --[in] 第一个参数
* second --[in] 第二参数
*
* Return: 执行后的结果
*
* Other:
*
*/
int add(int first,int second);
#endif

MyProgram.c

#include "MyProgram.h"

/*
* Description: 整数加法函数
* Function: add
* Parameter: first --[in] 第一个参数
* second --[in] 第二参数
*
* Return: 执行后的结果
*
* Other:
*
*/
int add(int first,int second)
{
return first+second;
}

  2、搭建测试环境,创建文件MainTest.cpp和FirstTestGroup.cpp文件,其中MainTest.cpp启动“入口函数”的作用,而FirstTestGroup.cpp即一个单元测试组;

MainTest.cpp

#include "D:\\cpputest-3.3\\include\\CppUTest\\CommandLineTestRunner.h"
int main(int ac, const char** av)
{
/* These checks are here to make sure assertions outside test runs don't crash */
CHECK(true);
LONGS_EQUAL(1, 1);
return CommandLineTestRunner::RunAllTests(ac, av);
}

FirstTestGroup.cpp

#include "D:\\cpputest-3.3\\include\\CppUTest\\TestHarness.h"
#include "D:\\cpputest-3.3\\include\\CppUTest\\TestOutput.h"
extern "C"{
#include "MyProgram.h"
} TEST_GROUP(MyProgram)
{
}; TEST(MyProgram, add)
{
LONGS_EQUAL(2,add(1,2));
}

  3、进行编译,链接,然后执行,其结果如图

VC++6.0 配置CppUTest测试环境

  四、拓展

  上述实例,仅演示了整数类型数据比对LONGS_EQUAL,更多数据类型数据比对参见README_CppUTest.txt。

上一篇:Linux下使用skipfish扫描网站漏洞步骤


下一篇:Windows Azure HandBook (5) Azure混合云解决方案