Google Mock这个链接包含googleTest项目,里面包含了google mock。所以一起安装GoogleMock和Goo
gleTest.
# 设置变量GMOCK_DIR和GTEST_DIR为我们源代码位置
# 生成gtest-all.o
g++ -isystem ${GTEST_DIR}/include -I${GTEST_DIR} \
-isystem ${GMOCK_DIR}/include -I${GMOCK_DIR} \
-pthread -c ${GTEST_DIR}/src/gtest-all.cc
# 生成gmock-all.o
g++ -isystem ${GTEST_DIR}/include -I${GTEST_DIR} \
-isystem ${GMOCK_DIR}/include -I${GMOCK_DIR} \
-pthread -c ${GMOCK_DIR}/src/gmock-all.cc
# 静态库libgmock.a
ar -rv libgmock.a gtest-all.o gmock-all.o
测试
g++ -isystem ${GTEST_DIR}/include -isystem ${GMOCK_DIR}/include \
-pthread path/to/your_test.cc libgmock.a -o your_test
./your_test
```![这里写图片描述](https://img-blog.csdn.net/20171201004555936?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VpeGluXzM2MjgzMDk1/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
<div class="se-preview-section-delimiter">div>
### 2.CppUTest
[cpputest](https://github.com/cpputest/cpputest)
<div class="se-preview-section-delimiter">div>
```shell
cd cpputest_build
autoreconf .. -i
../configure
make
make check
make install
测试
//main.cpp
#include "CppUTest/TestHarness.h"
#include "../inc/ClassName.h"
#include "CppUTest/CommandLineTestRunner.h"
#include "CppUTest/UtestMacros.h"
TEST_GROUP(FirstTestGroup)
{
;
};
TEST(FirstTestGroup, FirstTest)
{
CHECK(true);
CHECK_EQUAL(1,1);
LONGS_EQUAL(1,1);
DOUBLES_EQUAL(1.000, 1.001, .01);
STRCMP_EQUAL("hello", "hello");
FAIL("The prior tests pass, but this one doesn't");
}
int main(int ac, char** av)
{
return RUN_ALL_TESTS(ac, av);
}
编译
#makefile
.PHONY:clean all
CPPUTEST_HOME = /usr/local
WORKDIR=.
VPATH = ./src
CC=gcc
CXX=g++
CFLGS= -Wall -g -I$(WORKDIR)/inc
LIBFLAG = -L$(HOME)/lib
CPPFLAGS += -I$(CPPUTEST_HOME)/include -I$(WORKDIR)/inc
CXXFLAGS += -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorNewMacros.h
CFLAGS += -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorMallocMacros.h
LD_LIBRARIES = -L$(CPPUTEST_HOME)/lib -l CppUTest -l CppUTestExt
DIR_BIN=./bin
BIN = main
#testdbapi
all:$(BIN)
main:main.o
$(CXX) $^ -o $@ $(LD_LIBRARIES)
mv $@ $(DIR_BIN)
rm $^
%.o:%.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@ $(LD_LIBRARIES)
clean:
rm -f *.o $(BIN)
//main.cpp
#include "CppUTest/TestHarness.h"
#include "../inc/ClassName.h"
#include "CppUTest/CommandLineTestRunner.h"
#include "CppUTest/UtestMacros.h"
TEST_GROUP(FirstTestGroup)
{
;
};
TEST(FirstTestGroup, FirstTest)
{
CHECK(true);
CHECK_EQUAL(1,1.11);
LONGS_EQUAL(1,1);
DOUBLES_EQUAL(1.000, 1.001, .01);
STRCMP_EQUAL("hello", "hello");
FAIL("The prior tests pass, but this one doesn't");
}
int main(int ac, char** av)
{
return RUN_ALL_TESTS(ac, av);
}
编译
#makefile
.PHONY:clean all
CPPUTEST_HOME = /usr/local
WORKDIR=.
VPATH = ./src
CC=gcc
CXX=g++
CFLGS= -Wall -g -I$(WORKDIR)/inc
LIBFLAG = -L$(HOME)/lib
CPPFLAGS += -I$(CPPUTEST_HOME)/include -I$(WORKDIR)/inc
CXXFLAGS += -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorNewMacros.h
CFLAGS += -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorMallocMacros.h
LD_LIBRARIES = -L$(CPPUTEST_HOME)/lib -l CppUTest -l CppUTestExt
DIR_BIN=./bin
BIN = main
#testdbapi
all:$(BIN)
main:main.o
$(CXX) $^ -o $@ $(LD_LIBRARIES)
mv $@ $(DIR_BIN)
rm $^
%.o:%.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@ $(LD_LIBRARIES)
clean:
rm -f *.o $(BIN)
#include
#include
#include
using namespace std;
int ParseJsonFromString()
{
const char* str = "{\"user_id\": \"06141007\", \"passwd\": \"3a5cd\", \"user_name\": \"Tom\",\"degree\": 47}";
Json::Reader reader;
Json::Value root;
if (reader.parse(str, root)){
std::string user_id = root["user_id"].asString();
std::string passwd = root["passwd"].asString();
std::string user_name = root["user_name"].asString();
int degree = root["degree"].asInt();
cout<cout<if(passwd.compare("1") != 0){
cout<<"passwd error, please try again"<exit(1);
}
cout<cout<return 0;
}
int main()
{
ParseJsonFromString();
return 0;
}