直接在CMake項目中編譯GoogleTest和GoogleMock作為項目的一部分


直接在CMake項目中編譯GoogleTest和GoogleMock作為項目的一部分

本文是關於如何將GoogleTest和GoogleMock在沒有預先編譯安裝在機器的情況下,直接在項目中作為項目的一部分進行編譯。

目錄:

  1. 環境依賴
  2. GoogleTest和GoogleMock
  3. 在CMake項目中配置GoogleTest和GoogleMock

1. 環境依賴

Note: 環境只列出了本文實現過程中本人機器的配置,不代表最低配置

  • ubuntu 14.04 64位
  • CMake 2.8

2.GoogleTest和GoogleMock

Google Test是谷歌開源的C++ 單元測試框架,簡稱gtest。Google Mock是Google Test的一個擴展,是為了應用c++的模擬類。

3.在CMake項目中配置GoogleTest和GoogleMock

如果GoogleTest和GoogleMock已經實現安裝到機器上了,在CMake項目中使用GoogleTest和GoogleMock非常方便,只需要使用find_package()就可以使用它們,點擊查看詳情。但是如果不像在機器中安裝GoogleTest和GoogleMock並且希望項目在其他機器上也能夠正常的運行而不需要安裝GoogleTest和GoogleMock,那么使用find_package()這種方法就無能為力了。本文的前提是沒有安裝GoogleTest和GoogleMock且希望將GoogleTest和GoogleMock作為項目的一部分直接build到項目中。

為了達到這個目的,可以借用Crascit在github上提供的包下載模塊 中的DownloadProject.cmake和DownloadProject.CMakeLists.cmake.in文件,並且將其放在項目的頂層目錄中。並且在CMakeLists.txt文件中加入如下部分。

cmake_minimum_required(VERSION 2.8.2)

project(CPP_CMake_GoogleTest)

include(CTest)

if (CMAKE_VERSION VERSION_LESS 3.2)
  set(UPDATE_DISCONNECTED_IF_AVAILABLE "")
else()
  set(UPDATE_DISCONNECTED_IF_AVAILABLE "UPDATE_DISCONNECTED 1")
endif()

include(DownloadProject.cmake)
download_project(PROJ                googletest
                 PREFIX              CMAKE_SOURCE_DIR/third_parts/googletest
                 GIT_REPOSITORY      https://github.com/google/googletest.git
                 GIT_TAG             master
                 ${UPDATE_DISCONNECTED_IF_AVAILABLE}
)

# Prevent GoogleTest from overriding our compiler/linker options
# when building with Visual Studio
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR})

# When using CMake 2.8.11 or later, header path dependencies
# are automatically added to the gtest and gmock targets.
# For earlier CMake versions, we have to explicitly add the
# required directories to the header search path ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
  include_directories("${gtest_SOURCE_DIR}/include"
                      "${gmock_SOURCE_DIR}/include")
endif()

# Trivial example using gtest and gmock
add_executable(example example.cpp)
target_link_libraries(example gtest gmock_main)
add_test(NAME example_test COMMAND example)

download_project函數默認使用的是CMAKE_BINARY_DIR作為下載包和安裝包的位置,讀者可以通過PREFIX參數設置oogleTest和GoogleMock的位置。本人喜歡將第三方庫放在ProjectName/third_parts/package_name,故將PREFIX參數設置成CMAKE_SOURCE_DIR/third_parts/googletest。

編寫example進行測試,example.cpp代碼如下:

#include "gtest/gtest.h"
#include "gmock/gmock.h"


// Simple test, does not use gmock
TEST(Dummy, foobar)
{
    EXPECT_EQ(1, 1);
}


// Real class we want to mock
class TeaBreak
{
  public:
    virtual ~TeaBreak() {}

    // Return minutes taken to make the drinks
    int morningTea()
    {
      return makeCoffee(true,  1) +
           makeCoffee(false, 0.5) +
           makeHerbalTea();
    }

  private:
    virtual int makeCoffee(bool milk, double sugars) = 0;
    virtual int makeHerbalTea() = 0;
};

// Mock class
class MockTeaBreak : public TeaBreak
{
  public:
    MOCK_METHOD2(makeCoffee,    int(bool milk, double sugars));
    MOCK_METHOD0(makeHerbalTea, int());
};


using ::testing::Return;
using ::testing::_;

// Mocked test
TEST(TeaBreakTest, MorningTea)
{
  MockTeaBreak  teaBreak;
  EXPECT_CALL(teaBreak, makeCoffee(_,_))
      .WillOnce(Return(2))
      .WillOnce(Return(1));
  EXPECT_CALL(teaBreak, makeHerbalTea())
      .WillOnce(Return(3));

  EXPECT_LE(teaBreak.morningTea(), 6);
}

本文中項目結果如下圖所示:

項目代碼結構圖

進行編譯:

mkdir build
cd build
cmake ..
make

運行example,結果如下圖所示

example gtest 結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM