1. 编译

  1. # 下载
  2. https://github.com/google/googletest
  3. # tar zxvf googletest-release-1.10.0.tar.gz
  4. # cd googletest-release-1.10.0
  5. # mkdir build; cd build
  6. # cmake..
  7. # make -j 10
  8. 静态库
  9. # ll lib/libgtest*
  10. -rw-r--r--. 1 root root 2059300 Jun 18 16:14 lib/libgtest.a
  11. -rw-r--r--. 1 root root 4076 Jun 18 16:14 lib/libgtest_main.a
  12. 头文件
  13. # ll ../googletest/include/gtest
  14. -rw-rw-r--. 1 root root 14367 Oct 3 2019 gtest-death-test.h
  15. -rw-rw-r--. 1 root root 93924 Oct 3 2019 gtest.h
  16. -rw-rw-r--. 1 root root 27039 Oct 3 2019 gtest-matchers.h
  17. -rw-rw-r--. 1 root root 8011 Oct 3 2019 gtest-message.h
  18. -rw-rw-r--. 1 root root 22183 Oct 3 2019 gtest-param-test.h
  19. -rw-rw-r--. 1 root root 14850 Oct 3 2019 gtest_pred_impl.h
  20. -rw-rw-r--. 1 root root 34029 Oct 3 2019 gtest-printers.h
  21. -rw-rw-r--. 1 root root 2519 Oct 3 2019 gtest_prod.h
  22. -rw-rw-r--. 1 root root 10097 Oct 3 2019 gtest-spi.h
  23. -rw-rw-r--. 1 root root 6853 Oct 3 2019 gtest-test-part.h
  24. -rw-rw-r--. 1 root root 15395 Oct 3 2019 gtest-typed-test.h
  25. drwxrwxr-x. 3 root root 251 Oct 3 2019 internal

2. 写测试工程

  1. cmake_minimum_required (VERSION 2.8)
  2. set(ORIGIN_PATH ${PROJECT_SOURCE_DIR})
  3. message(STATUS "origin path :" ${ORIGIN_PATH})
  4. set(DPDKVER 18.02.1)
  5. set(CMAKE_C_COMPILER g++)
  6. set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_LIST_DIR}/bin)
  7. set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_LIST_DIR}/lib)
  8. set(SYSLIB pthread rt z m nsl dl numa)
  9. add_definitions(" -fpermissive -march=native -w -enable-threads=posix -DLINUX ")
  10. add_definitions(" -std=c++11 ")
  11. #头文件
  12. include_directories(${CMAKE_CURRENT_LIST_DIR}/include)
  13. include_directories(${ORIGIN_PATH}/include/3rdinc/gtest)
  14. include_directories(${ORIGIN_PATH}/include/pub)
  15. include_directories(${ORIGIN_PATH}/include/middle)
  16. include_directories(${ORIGIN_PATH}/include/common)
  17. include_directories(${ORIGIN_PATH}/include/util)
  18. include_directories(${ORIGIN_PATH}/include/3rdinc/dpdk/${DPDKVER})
  19. include_directories(${ORIGIN_PATH}/include/3rdinc/vty)
  20. include_directories(${ORIGIN_PATH}/include/3rdinc/hyperscan)
  21. include_directories(${ORIGIN_PATH}/include/export)
  22. include_directories(${ORIGIN_PATH}/include/counter)
  23. include_directories(${ORIGIN_PATH}/include/serializer)
  24. #源文件
  25. aux_source_directory(${ORIGIN_PATH}/packages/middle/src DIR_LIB_MIDDLE)
  26. #静态库
  27. link_directories(${ORIGIN_PATH}/3rdlib/gtest)
  28. add_library(gmiddle ${DIR_LIB_MIDDLE})
  29. #构建
  30. aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/src DIR_SRC_MIDDLE)
  31. add_executable(MockMiddle ${DIR_SRC_MIDDLE})
  32. #指定库依赖关系
  33. target_link_libraries(MockMiddle gtest gtest_main gmiddle)
  34. target_link_libraries(MockMiddle ${SYSLIB})

3. 运行测试工程

  1. # google单元测试
  2. option(gtest_build "option for gtest" OFF)
  3. if (gtest_build)
  4. message(STATUS "gtest_build on")
  5. add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/gtest)
  6. endif (gtest_build)

4. 参考文献