资源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://blog.csdn.net/qq_26437925/article/details/56479841
下面是ubuntu 14.04 32bit, gmock测试 共6个文件
参考
http://blog.csdn.net/gubenpeiyuan/article/details/50678697
#ifndef SRC_MESSENGER_H_
#define SRC_MESSENGER_H_
#include
using namespace std;
class Messenger
{
public:
virtual ~Messenger() {}
virtual string getMessage() = 0;
virtual int add(int a, int b) = 0;
};
#endif /* SRC_MESSENGER_H_ */
#ifndef SRC_HELLOWORLD_H_
#define SRC_HELLOWORLD_H_
#include
#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_ */
#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 a, int b) const
{
return messenger->add(a, b);
}
#ifndef SRC_MOCKMESSENGER_H_
#define SRC_MOCKMESSENGER_H_
#include "Messgener.h"
#include
#include
using namespace std;
class MockMessenger : public Messenger
{
public:
MOCK_METHOD0(getMessage, string());
MOCK_METHOD2(add, int(int, int));
};
#endif /* SRC_MOCKMESSENGER_H_ */
#include "HelloWorld.h"
#include
#include
#include "MockMessgener.h"
#include
#include
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));
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));
}
.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)
运行截图
gmock学习参考
http://blog.csdn.net/jfkidear/article/details/17565669