C++ Programming with TDD之一:GMOCK框架简介
所谓测试驱动开发,英文全称Test-Driven Development,简称TDD,是一种不同于传统软件开发流程的新型的开发方法。就是在明确要开发某个功能后,首先思考如何对这个功能进行测试,并完成测试代码的编写,然后编写相关的代码满足这些测试用例。然后循环进行添加其他功能,直到完成全部功能的开发。
Google Mock的设计灵感来源于jMock和EasyMock,它的作用是帮你快速地做出一个接口的仿制品。如果你的设计依赖其它的类,而这些类还没有完成或非常昂贵(如数据库);如果你要测试你的模块与其它模块是否能正确结合,并想了解其交互过程;那么Google Mock就能帮助你。
PS: The official Google Mock site is https://code.google.com/p/googlemock/. The version used for building the examples is Google Mock 1.7.0.
一、 环境配置
gmock1.7.0中使用了C++11新标准,所以我们的编译器需要支持C++11才行,在Linux系统中,即需要安装GCC4.7/G++4.7,我的测试环境是Ubuntu12.04,默认安装的是GCC4.6/G++4.6,所以需要在安装编译gmock之前首先安装GCC4.7/G++4.7,这里也顺便把安装的过程加上,有需要的猿们可以参考:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-4.7 g++-4.7
安装成功后我们如果要使用gcc-4.7&g++-4.7来编译的话,我们就得把gcc改为gcc-4.7,g++同理,改为g++-4.7来进行编译.如果你想直接使用gcc-4.7而不改变编译时gcc改为gcc-4.7的话,我们就可以更改一下gcc的软链接:
sudo rm /usr/bin/gcc
sudo ln -s /usr/bin/gcc-4.7 /usr/bin/gcc
sudo rm /usr/bin/g++
sudo ln -s /usr/bin/g++-4.7 /usr/bin/g++
PS: 在平时使用的时候如果使用C++0X标准,记得加-std=c++11。
二、gmock安装
下载好gmock之后,解压,然后切换到gmock源码所在目录,使用如下命令安装:
mkdir mybuild
cd mybuild
cmake ..
make
同时,你还需要编译google test,其包含在gmock源码下的gtest文件夹,切换到gtest文件夹,然后用相同的方式安装即可。
三、实例
Soundex.h文件:
#ifndef Soundex_h
#define Soundex_h
#include <string> class Soundex
{
public:
std::string encode(const std::string& word) const {
return zeroPad(word);
} private:
std::string zeroPad(const std::string& word) const {
return word + "";
}
}; #endif
SoundexTest.cpp文件:
#include "gmock/gmock.h"
#include "Soundex.h" using namespace testing; class SoundexEncoding: public Test {
public:
Soundex soundex;
}; TEST_F(SoundexEncoding, RetainsSoleLetterOfOneLetterWord) {
ASSERT_THAT(soundex.encode("A"), Eq("A000"));
} TEST_F(SoundexEncoding, PadsWithZerosToEnsureThreeDigits) {
ASSERT_THAT(soundex.encode("I"), Eq("I000"));
}
main.cpp文件:
#include "gmock/gmock.h" int main(int argc, char** argv) {
testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
CMakeLists.txt文件:
project(chapterFirstExample)
cmake_minimum_required(VERSION 2.6) include_directories($ENV{GMOCK_HOME}/include $ENV{GMOCK_HOME}/gtest/include)
link_directories($ENV{GMOCK_HOME}/mybuild $ENV{GMOCK_HOME}/gtest/mybuild)
add_definitions(-std=c++0x)
set(CMAKE_CXX_FLAGS "${CMAXE_CXX_FLAGS} -Wall") set(sources
main.cpp
SoundexTest.cpp)
add_executable(test ${sources})
target_link_libraries(test pthread)
target_link_libraries(test gmock)
target_link_libraries(test gtest)
好了,编译执行吧,执行结果如下:
好了gmock的使用就介绍到这里,需要深入研究的童鞋可以参考官方文档。这里最重要的不是学会使用gmock,而是要在学会使用gmock之后养成TDD开发的好习惯.
Test-driving vs Testing: Using a testing technique, you would seek to exhaustively analyze the specification in question (and possibly the code) and devise tests that exhaustively cover the behavior. TDD is instead a technique for driving the design of the code. In TDD, you write tests to describe the next behavior needed.
最新文章
- 【转】Tomcat的默认访问路径
- 文件权限及特殊权限管理SUID、SGID和Sticky
- java javacv调用摄像头并拍照
- Qt-note0906
- knockoutJS+knockout.multimodels使用记录
- Datagrid扩展方法InitEditGrid{支持单元格编辑}
- C# Hashtable 简述
- HDU4514(非连通图的环判断与图中最长链)
- Windows server 2008 R2实现多用户远程连接
- php 备份数据库
- IWorkSpace接口介绍 1.打开各种数据库
- vuex - 项目结构目录及一些简单配置
- js reduce()方法使用
- localforage调用setItem时出现DOMException错误的解决方法
- json对象组按某个字段排序
- Windows10 bypassUAC绕过用户账户控制
- GSP事件探查器 无法进行跟踪的解决办法(场景之一)
- 【JS】【2】ajax传的参数为数组时,后台接收为null的处理
- python获取命令行参数的方法(汇总)
- Android -- Interpolator
热门文章
- 【杂题总汇】UVa-1627 Team them up!
- struts2入门第一天----------配置环境
- iPhone 横屏时默认会放大文字的问题
- ethereum(以太坊)(六)--整型(int)
- php-5.6.26源代码 - opcode处理器的注入
- C语言数组篇(四)二维数组
- 笔记-python-standard library-11.2 os.path
- is 和 == 的区别,utf和gbk的转换,join用法
- 为什么不要使用 Async Void ?
- Android开发——事件分发机制详解