Google Test separate project - How to get tests ru

2019-02-19 10:43发布

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 )

1条回答
淡お忘
2楼-- · 2019-02-19 11:03

The problem seems to be the organization of your code modules. Supposed you have a c++ target project, that provides an executable program as it's final output:

  • I suppose you want to create two executable artifacts
    • your final application
    • a test runner application that runs all test cases you have specified
  • As for this should be your misconception, how to setup this scenario properly:
    You cannot link functions from an executable artifact (the application) into another one (the test runner).
  • You can either
    • provide the core classes separate in a library and link it to your final application and the test runner. The application should provide a thin wrapper from the main() entry point.
    • add links to the sources for the classes under test, and completely compile them in the test runner environment
查看更多
登录 后发表回答