4

I am trying to figure out how to run Google Test against my C++ project using CMake. So far I have created a project called Simple and a Google Test project called SimpleTest.

For the Simple Project

Here's my CMakeLists.txt file:

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})

Here's my main.cpp file:

#include <iostream>

#include "NewCppClass.h"

using namespace std;

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

Here is my Class Header:

#ifndef SIMPLE_NEWCPPCLASS_H
#define SIMPLE_NEWCPPCLASS_H

class NewCppClass {
    public:
        int getNumberToTest();

};

#endif //SIMPLE_NEWCPPCLASS_H

Here is my .cpp file:

#include "NewCppClass.h"

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

For the SimpleTest Project

Here's my CMakeLists.txt file:

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})

Here's my Main_TestAll.cpp file:

#include "gtest/gtest.h"

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

Here is MyFirstTest.cpp file:

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

UPDATE:


πάντα-ῥεῖ wrote this 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.

I think this is the correct direction, so I'm adding it to the question as a reminder to myself and it may be helpful to others.

Also in below, written by πάντα-ῥεῖ:

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

I'm including all of this information here as I try to compile in my mind what needs to be done.


If I understand what needs to be done correctly, then I need to (in my C++ project) add to the CMakeLists.txt file to add GTest as an ExternalProject and add the tests in add_executable. Something like this:

################################
# 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 )
4

1 回答 1

5

问题似乎是您的代码模块的组织。假设您有一个 c++ 目标项目,它提供了一个可执行程序作为其最终输出:

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