C++ Programming with TDD之一:GMOCK框架简介
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++ Programming with TDD之一:GMOCK框架简介,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3077字,纯文字阅读大概需要5分钟。
内容图文
所谓测试驱动开发,英文全称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,这里也顺便把安装的过程加上,有需要的猿们可以参考:
1 sudo add-apt-repository ppa:ubuntu-toolchain-r/test 2sudo apt-get update 3sudo apt-get installgcc-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的软链接:
1 sudo rm /usr/bin/gcc2sudoln -s /usr/bin/gcc-4.7 /usr/bin/gcc3sudorm /usr/bin/g++ 4sudoln -s /usr/bin/g++-4.7 /usr/bin/g++
PS: 在平时使用的时候如果使用C++0X标准,记得加 -std=c++11。
二、gmock安装
下载好gmock之后,解压,然后切换到gmock源码所在目录,使用如下命令安装:
1 mkdir mybuild 2 cd mybuild 3 cmake .. 4 make
同时,你还需要编译google test,其包含在gmock源码下的gtest文件夹,切换到gtest文件夹,然后用相同的方式安装即可。
三、实例
Soundex.h文件:
1 #ifndef Soundex_h 2 #define Soundex_h 3 #include <string> 4 5class Soundex 6{ 7public: 8 std::string encode(const std::string& word) const { 9return zeroPad(word); 10 } 1112private: 13 std::string zeroPad(const std::string& word) const { 14return word + "000"; 15 } 16}; 1718#endif
SoundexTest.cpp文件:
1 #include "gmock/gmock.h" 2 #include "Soundex.h" 3 4usingnamespace testing; 5 6class SoundexEncoding: public Test { 7public: 8 Soundex soundex; 9}; 1011TEST_F(SoundexEncoding, RetainsSoleLetterOfOneLetterWord) { 12 ASSERT_THAT(soundex.encode("A"), Eq("A000")); 13} 1415TEST_F(SoundexEncoding, PadsWithZerosToEnsureThreeDigits) { 16 ASSERT_THAT(soundex.encode("I"), Eq("I000")); 17 }
main.cpp文件:
1 #include "gmock/gmock.h"23int main(int argc, char** argv) { 4 testing::InitGoogleMock(&argc, argv); 5return RUN_ALL_TESTS(); 6 }
CMakeLists.txt文件:
1 project(chapterFirstExample) 2 cmake_minimum_required(VERSION 2.6) 3 4 include_directories($ENV{GMOCK_HOME}/include $ENV{GMOCK_HOME}/gtest/include) 5 link_directories($ENV{GMOCK_HOME}/mybuild $ENV{GMOCK_HOME}/gtest/mybuild) 6 add_definitions(-std=c++0x) 7set(CMAKE_CXX_FLAGS "${CMAXE_CXX_FLAGS} -Wall") 8 9set(sources 10 main.cpp 11 SoundexTest.cpp) 12add_executable(test ${sources}) 13target_link_libraries(test pthread) 14target_link_libraries(test gmock) 15 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.
原文:http://www.cnblogs.com/berlin-sun/p/gmock.html
内容总结
以上是互联网集市为您收集整理的C++ Programming with TDD之一:GMOCK框架简介全部内容,希望文章能够帮你解决C++ Programming with TDD之一:GMOCK框架简介所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。