簡體   English   中英

Google測試獨立項目-如何使測試針對C ++項目運行

[英]Google Test separate project - How to get tests running against the C++ project

我試圖弄清楚如何使用CMake對我的C ++項目運行Google Test。 到目前為止,我已經創建了一個名為Simple的項目和一個名為SimpleTest的Google Test項目。

對於簡單項目

這是我的CMakeLists.txt文件:

cmake_minimum_required(VERSION 2.8.4)
project(Simple)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES
    main.cpp
    NewCppClass.cpp
    NewCppClass.h)

add_executable(Simple ${SOURCE_FILES})

這是我的main.cpp文件:

#include <iostream>

#include "NewCppClass.h"

using namespace std;

int main() {
    NewCppClass newCppClass;
    int i = newCppClass.getNumberToTest();
    cout << "i = " << i;
    return 0;
}

這是我的班級標題:

#ifndef SIMPLE_NEWCPPCLASS_H
#define SIMPLE_NEWCPPCLASS_H

class NewCppClass {
    public:
        int getNumberToTest();

};

#endif //SIMPLE_NEWCPPCLASS_H

這是我的.cpp文件:

#include "NewCppClass.h"

int NewCppClass::getNumberToTest() {
    return 5;
}

對於SimpleTest項目

這是我的CMakeLists.txt文件:

cmake_minimum_required(VERSION 2.8.4)
project(SimpleTest)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES
    Main_TestAll.cpp
    MyFirstTest.cpp)

enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

add_executable(SimpleTest ${SOURCE_FILES})

target_link_libraries(SimpleTest ${GTEST_BOTH_LIBRARIES})

這是我的Main_TestAll.cpp文件:

#include "gtest/gtest.h"

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

這是MyFirstTest.cpp文件:

當然,當我弄清楚如何指向我的Simple項目時,這個include必須更改。

#include "this/package/NewCppClass.h"
#include "gtest/gtest.h"

namespace {

// The fixture for testing class NewCppClass.
    class NewCppClassTest : public ::testing::Test {
    protected:
        // You can remove any or all of the following functions if its body
        // is empty.

        NewCppClassTest() {
            // You can do set-up work for each test here.
        }

        virtual ~NewCppClassTest() {
            // You can do clean-up work that doesn't throw exceptions here.
        }

        // If the constructor and destructor are not enough for setting up
        // and cleaning up each test, you can define the following methods:

        virtual void SetUp() {
            // Code here will be called immediately after the constructor (right
            // before each test).
        }

        virtual void TearDown() {
            // Code here will be called immediately after each test (right
            // before the destructor).
        }

        // Objects declared here can be used by all tests in the test case for Foo.
    };

// Tests that NewCppClass::getNumberToTest() is not equal to this fixed mumber.
    TEST_F(NewCppClassTest, ThisTestShallFail) {
        NewCppClass newCppClass;
        int i = newCppClass.getNumberToTest();
        EXPECT_EQ(i, 2);
    }

}  // namespace

更新:


πάντα-ῥεῖ寫下了這個問題

我建議將所有測試用例類(作為純.cpp源代碼)放入一個單獨的項目中,並與一個單獨的庫項目中的被測類鏈接。 在main()函數中包含gtest_all.cc,或者在測試項目中包含指向gtest庫的鏈接。

要運行測試用例,請添加從該項目運行UnitTester工件構建作為附加的構建步驟。

我認為這是正確的方向,因此我將其添加到問題中以提醒自己,這可能對其他人有所幫助。

同樣在下面,由πάντα-ῥεῖ撰寫:

...被測類應捆綁到單獨的庫工件中,並鏈接到測試運行器應用程序。

我將所有這些信息都包括在這里,因為我試圖在腦海中匯編需要做什么。


如果我了解需要正確執行的操作,則需要(在我的C ++項目中)添加到CMakeLists.txt文件中,以將GTest添加為ExternalProject並在add_executable中添加測試。 像這樣:

################################
# GTest
################################
include(ExternalProject)
enable_testing()
find_package(GTest REQUIRED)

################################
# Unit Tests
################################
# Add test cpp file
# Link test executable against gtest & gtest_main
add_executable(SimpleTest
    Main_TestAll.cpp
    MyFirstTest.cpp)
target_link_libraries(Test GTest)
add_test( runUnitTests runUnitTests )

問題似乎在於代碼模塊的組織。 假設您有一個c ++目標項目,該項目提供一個可執行程序作為最終輸出:

  • 我想您想創建兩個可執行工件
    • 您的最終申請
    • 運行您指定的所有測試用例的測試運行器應用程序
  • 至於這應該是您的誤解,如何正確設置此方案:
    您不能將功能從可執行工件(應用程序)鏈接到另一個工件(測試運行器)。
  • 你可以
    • 在庫中單獨提供核心類,並將其鏈接到最終應用程序和測試運行程序。 應用程序應從main()入口點提供一個薄包裝器。
    • 為被測類添加源鏈接,並在測試運行器環境中完全編譯它們

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM