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 )