c++ - Google 测试独立项目 - 如何让测试针对 C++ 项目运行

标签 c++ cmake googletest clion

我正在尝试了解如何使用 CMake 针对我的 C++ 项目运行 Google 测试。到目前为止,我已经创建了一个名为 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;
}

这是我的类标题:

#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

更新:


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

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++ 目标项目,它提供了一个可执行程序作为它的最终输出:

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

关于c++ - Google 测试独立项目 - 如何让测试针对 C++ 项目运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30041576/

相关文章:

c - Windows cmake 找不到 memset 或其他标准函数

c++ - CMake:将 GTest 添加到构建中

c++ - 使用 Mingw-w64 编译谷歌测试

c++ - 为什么 `SetUp`/`TearDown`在gtest中是虚拟的?

c++ - FLTK:覆盖 FL_box 构造函数。 C++

c# - 计算功能限制的最佳方法是什么?

C++ 比较器保证和内部工作

c++ - CMake:如何根据不同的主条目进行编译

c++ - 是 unsigned char ('0' ) 合法的 C++

cmake - 如何告诉 CMake 链接到源目录中的静态库?