1. 编译
# 下载
https://github.com/google/googletest
# tar zxvf googletest-release-1.10.0.tar.gz
# cd googletest-release-1.10.0
# mkdir build; cd build
# cmake..
# make -j 10
静态库
# ll lib/libgtest*
-rw-r--r--. 1 root root 2059300 Jun 18 16:14 lib/libgtest.a
-rw-r--r--. 1 root root 4076 Jun 18 16:14 lib/libgtest_main.a
头文件
# ll ../googletest/include/gtest
-rw-rw-r--. 1 root root 14367 Oct 3 2019 gtest-death-test.h
-rw-rw-r--. 1 root root 93924 Oct 3 2019 gtest.h
-rw-rw-r--. 1 root root 27039 Oct 3 2019 gtest-matchers.h
-rw-rw-r--. 1 root root 8011 Oct 3 2019 gtest-message.h
-rw-rw-r--. 1 root root 22183 Oct 3 2019 gtest-param-test.h
-rw-rw-r--. 1 root root 14850 Oct 3 2019 gtest_pred_impl.h
-rw-rw-r--. 1 root root 34029 Oct 3 2019 gtest-printers.h
-rw-rw-r--. 1 root root 2519 Oct 3 2019 gtest_prod.h
-rw-rw-r--. 1 root root 10097 Oct 3 2019 gtest-spi.h
-rw-rw-r--. 1 root root 6853 Oct 3 2019 gtest-test-part.h
-rw-rw-r--. 1 root root 15395 Oct 3 2019 gtest-typed-test.h
drwxrwxr-x. 3 root root 251 Oct 3 2019 internal
2. 写测试工程
cmake_minimum_required (VERSION 2.8)
set(ORIGIN_PATH ${PROJECT_SOURCE_DIR})
message(STATUS "origin path :" ${ORIGIN_PATH})
set(DPDKVER 18.02.1)
set(CMAKE_C_COMPILER g++)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_LIST_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_LIST_DIR}/lib)
set(SYSLIB pthread rt z m nsl dl numa)
add_definitions(" -fpermissive -march=native -w -enable-threads=posix -DLINUX ")
add_definitions(" -std=c++11 ")
#头文件
include_directories(${CMAKE_CURRENT_LIST_DIR}/include)
include_directories(${ORIGIN_PATH}/include/3rdinc/gtest)
include_directories(${ORIGIN_PATH}/include/pub)
include_directories(${ORIGIN_PATH}/include/middle)
include_directories(${ORIGIN_PATH}/include/common)
include_directories(${ORIGIN_PATH}/include/util)
include_directories(${ORIGIN_PATH}/include/3rdinc/dpdk/${DPDKVER})
include_directories(${ORIGIN_PATH}/include/3rdinc/vty)
include_directories(${ORIGIN_PATH}/include/3rdinc/hyperscan)
include_directories(${ORIGIN_PATH}/include/export)
include_directories(${ORIGIN_PATH}/include/counter)
include_directories(${ORIGIN_PATH}/include/serializer)
#源文件
aux_source_directory(${ORIGIN_PATH}/packages/middle/src DIR_LIB_MIDDLE)
#静态库
link_directories(${ORIGIN_PATH}/3rdlib/gtest)
add_library(gmiddle ${DIR_LIB_MIDDLE})
#构建
aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/src DIR_SRC_MIDDLE)
add_executable(MockMiddle ${DIR_SRC_MIDDLE})
#指定库依赖关系
target_link_libraries(MockMiddle gtest gtest_main gmiddle)
target_link_libraries(MockMiddle ${SYSLIB})
3. 运行测试工程
# google单元测试
option(gtest_build "option for gtest" OFF)
if (gtest_build)
message(STATUS "gtest_build on")
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/gtest)
endif (gtest_build)
4. 参考文献