Google 测试单独的项目 - 如何获得针对 C++ 项目的测试 运行

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

我正在尝试弄清楚如何 运行 Google 使用 CMake 针对我的 C++ 项目进行测试。到目前为止,我已经创建了一个名为 Simple 的项目和一个名为 SimpleTest 的 Google 测试项目。

对于简单项目

这是我的 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;
}

这是我的 Class Header:

#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文件:

Of course, this include must change when I figure out how to point to my Simple project.

#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

更新:


πìντα-ῥεῖ 写了这个 :

I would recommend to put all the test case classes (as plain .cpp sources) into a separate project, and link with the classes under test from a separate library project. Include gtest_all.cc with the main() function, or link against the gtest library, with the test project.

To run the test cases add running the UnitTester artifact build from that project as an additional build step.

我认为这是正确的方向,所以我将其添加到问题中作为对自己的提醒,它可能对其他人有帮助。

同样在下方,由 πìντα-ῥεῖ 撰写:

...the classes under test should be bundled into a separate library artifact, and linked to the test runner application.

我在这里包含了所有这些信息,因为我试图在脑海中整理需要完成的事情。


如果我明白需要正确完成什么,那么我需要(在我的 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++ 目标项目,它提供了一个可执行程序作为它的最终输出:

  • 我想你想创建两个可执行工件
    • 你的最终申请
    • 运行您指定的所有测试用例的测试运行器应用程序
  • 这应该是你的误解,如何正确设置这个场景:
    您不能 link 从可执行工件(应用程序)到另一个工件(测试运行器)的功能。
  • 你可以
    • 在库中提供核心 类 并 link 将其提供给您的最终应用程序和测试运行器。应用程序应从 main() 入口点提供一个精简包装器。
    • 将link添加到被测类的源代码中,并在测试运行环境中完全编译它们