• 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,这里也顺便把安装的过程加上,有需要的猿们可以参考:

    1

    1 sudo add-apt-repository ppa:ubuntu-toolchain-r/test
    2 sudo apt-get update
    3 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的软链接:

    1 sudo rm /usr/bin/gcc
    2 sudo ln -s /usr/bin/gcc-4.7 /usr/bin/gcc
    3 sudo rm /usr/bin/g++
    4 sudo ln -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 
     5 class Soundex
     6 {
     7 public:
     8    std::string encode(const std::string& word) const {
     9       return zeroPad(word);
    10    }
    11 
    12 private:
    13    std::string zeroPad(const std::string& word) const {
    14       return word + "000";
    15    }
    16 };
    17 
    18 #endif

    SoundexTest.cpp文件:

     1 #include "gmock/gmock.h" 
     2 #include "Soundex.h"
     3 
     4 using namespace testing;
     5 
     6 class SoundexEncoding: public Test {
     7 public:
     8    Soundex soundex;
     9 };
    10 
    11 TEST_F(SoundexEncoding, RetainsSoleLetterOfOneLetterWord) {
    12    ASSERT_THAT(soundex.encode("A"), Eq("A000")); 
    13 }
    14 
    15 TEST_F(SoundexEncoding, PadsWithZerosToEnsureThreeDigits) {
    16    ASSERT_THAT(soundex.encode("I"), Eq("I000"));
    17 }

    main.cpp文件:

    1 #include "gmock/gmock.h"
    2 
    3 int main(int argc, char** argv) {
    4     testing::InitGoogleMock(&argc, argv);
    5     return 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)
     7 set(CMAKE_CXX_FLAGS "${CMAXE_CXX_FLAGS} -Wall")
     8 
     9 set(sources 
    10    main.cpp 
    11    SoundexTest.cpp)
    12 add_executable(test ${sources})
    13 target_link_libraries(test pthread)
    14 target_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.

  • 相关阅读:
    Python之路Day02
    Python之路Day01
    Python 常用单词
    JS-向数组添加元素
    JS-lambda表达式
    正则表达式问记录
    JDBC学习
    jquery常用选择器
    mongodb学习
    java8 lambda表达式
  • 原文地址:https://www.cnblogs.com/berlin-sun/p/gmock.html
Copyright © 2020-2023  润新知