ubuntu下 google gmock使用

前端之家收集整理的这篇文章主要介绍了ubuntu下 google gmock使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

资源github和网上,但是发现大部分都直接给出样例和运行图,windows下不给环境配置,linux下的不给makefile(或者不单独拎出来),太坑。
下文是ubuntu14.04 32bit + gmock1.8.0实践,给出了完整代码包括Makefile)和截图,如有错误,望指出.


环境配置

直接github下载最新版
https://github.com/google/googletest/tree/release-1.8.0


ubuntu下解压后

$ ./travis.sh
$ cmake ./CMakeLists.txt
$ make
$ sudo make install

安装成功后(.h .lib)

john@ubuntu:/usr/local/include$ ls
gmock  gtest  lauxlib.h  luaconf.h  lua.h  lua.hpp  luajit-2.1  lualib.h
john@ubuntu:/usr/local/include$ 
john@ubuntu:/usr/local/lib$ ls
libgmock.a       liblua.a            libluajit-5.1.so.2.1.0  pkgconfig
libgmock_main.a  libluajit-5.1.a     lua                     python2.7
libgtest.a       libluajit-5.1.so    luarocks                python3.4
libgtest_main.a  libluajit-5.1.so.2  node_modules
john@ubuntu:/usr/local/lib$ 

gtest使用还可以参考我的博文,博文后面有github学习笔记地址
http://www.jb51.cc/article/p-tbvtmtce-wk.html


gmock实践

下面是ubuntu 14.04 32bit,gmock测试 共6个文件
参考
http://www.jb51.cc/article/p-fjbdmjgq-vb.html

  • Messgener.h(未实现的接口)
#ifndef SRC_MESSENGER_H_
#define SRC_MESSENGER_H_

#include <string>
using namespace std;

class Messenger
{
public:
    virtual ~Messenger() {}
    virtual string getMessage() = 0;
    virtual int add(int a,int b) = 0;
};

#endif /* SRC_MESSENGER_H_ */
  • HelloWorld.h (调用了Messgener类)
#ifndef SRC_HELLOWORLD_H_
#define SRC_HELLOWORLD_H_

#include <string>
#include "Messgener.h"
using namespace std;

class HelloWorld
{
public:
    HelloWorld();
    virtual ~HelloWorld();
    string getMessage(Messenger* messenger) const;
    int hello_add(Messenger* messenger,int a,int b) const;
};

#endif /* SRC_HELLOWORLD_H_ */
  • HelloWord.cpp
#include "HelloWorld.h"
#include "Messgener.h"

HelloWorld::HelloWorld()
{
}

HelloWorld::~HelloWorld()
{
}

string HelloWorld::getMessage(Messenger* messenger) const
{
    return messenger->getMessage();
}

int HelloWorld::hello_add(Messenger* messenger,int b) const
{
    return messenger->add(a,b);
}
  • MockMessgener.h 写未实现的Mock类
#ifndef SRC_MOCKMESSENGER_H_
#define SRC_MOCKMESSENGER_H_

#include "Messgener.h"
#include <string>
#include <gmock/gmock.h>
using namespace std;

class MockMessenger : public Messenger
{
public:
    MOCK_METHOD0(getMessage,string());
    MOCK_METHOD2(add,int(int,int));
};

#endif /* SRC_MOCKMESSENGER_H_ */
  • main.cpp 测试
#include "HelloWorld.h"
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "MockMessgener.h"
#include <string>
#include <memory>
using namespace testing;

TEST(HelloWorldTest,getMessage)
{
    MockMessenger messenger;
    std::string msg = "Hello World";
    EXPECT_CALL(messenger,getMessage()).WillRepeatedly(Return(ByRef(msg)));

    HelloWorld helloWorld;
    EXPECT_EQ("Hello World",helloWorld.getMessage(&messenger));
    EXPECT_EQ("Hello World",helloWorld.getMessage(&messenger));
}


TEST(HelloWorldTest,add)
{
    MockMessenger messenger;
    int a = 3;
    int b = 11;
    EXPECT_CALL(messenger,add(a,b)).WillRepeatedly(Return(a+b));

    HelloWorld helloWorld;
    EXPECT_EQ(14,helloWorld.hello_add(&messenger,a,b));
}
  • Makefile
.PHONY: all clean

CC=g++
OBJ_DIR=./obj
HEADERS=-I.
DEBUG=-g -ggdb
WALL=-Wall -W
CFLAGS=$(WALL) $(DEBUG)
L_CC=$(CC) $(CFLAGS) $(HEADERS)

C_SRC=${wildcard *.cpp}
C_OBJ=$(patsubst %.cpp,$(OBJ_DIR)/%.o,$(C_SRC)) #目标文件
C_EXE=a.out

all:prepare $(C_EXE)

prepare:
    @if [ ! -d $(OBJ_DIR)  ];then mkdir $(OBJ_DIR); fi

$(C_EXE):$(C_OBJ)
    $(L_CC) $^ -o $@ -lgtest -lgtest_main -lgmock -lgmock_main -lpthread

$(OBJ_DIR)/%.o:%.cpp
    $(L_CC) -c $< -o $@

clean:
    @-rm -f $(C_EXE)
    @-rm -rf $(OBJ_DIR)

运行截图

原文链接:https://www.f2er.com/ubuntu/353686.html

猜你在找的Ubuntu相关文章