Google Test/Google Mock with Eclipse CDT and Cmake
How to setup Google Mock with Eclipse CDT and CMake. Google Mock also includes Google Test.
Create a new C++ project in Eclipse CDT
- Start Eclipse CDT
- Select workspace folder
- Close the Welcome screen
- Select “File - New - Other…”
- Select “C/C++ - C++ Project”
- Enter Project Name:
TestProj
- Select Project Type ‘‘Makefile project - Empty Project’’ and toolchain ‘‘Linux GCC’’
- Click “Finish”
- Select Yes if asked to open the C/C++ perspective
Build Google Test and Google Mock in Eclipse using CMake
Now we will import Google test and Google mock into our Eclipse project and build them with a minimalistic main file.
- Create a folder with name google on your harddrive
- Download Google Mock zip file (https://googlemock.googlecode.com/files/gmock-1.7.0.zip) to your new google folder and unpack
- Right click on the TestProj project in Project Explorer and select New - Folder
- Enter folder name google and click Finish
- Right click on the google folder and select Import…
- Select General - File system and click Next
- Click Browse…
- Select the google folder that you previously downloaded Google Mock to
- Check the google folder and click Finish
- You should now have a gmock-1.7.0 folder inside the google folder in the Project Explorer
- Create a new folder test by right clicking on TestProj and selecting New - Folder
- Right click on the test folder and select New - Source File
- Enter name main.cpp and click on Finish
- Add the following code to main.cpp and save:
#include "gmock/gmock.h" int main(int argc, char* argv[]) { ::testing::InitGoogleMock(&argc, argv); return RUN_ALL_TESTS(); }
- Right click on the TestProj project and select New - File
- Select TestProj root folder as Parent Directory, enter name “CMakeLists.txt” and click Finish
- Add the following to the CMakeLists.txt and save:
#Set minimum CMake version required to run this file cmake_minimum_required(VERSION 2.8) #Set name of project project("TestProj") #Set compiler flags: # std=c+11 <-- Add support for C++11 features # g3 <-- Include debugging information in build # Wall <-- Enable all compiler warnings messages add_definitions(-Wall -g3 -std=c++11) #Add the given directories to those the compiler uses to search for include files # CMAKE_CURRENT_SOURCE_DIR <-- This is the directory where the currently processed CMakeLists.txt is located in include_directories(${CMAKE_CURRENT_SOURCE_DIR}) #Add a subdirectory to the build. The directory specified must contain a CMakeLists.txt file. add_subdirectory(test)
- Create another CMakeLists.txt file inside the test folder
- Add the following to the CMakeLists.txt file inside the test folder
#Set a cache and an environment variable set(GMOCK_DIR "../google/gmock-1.7.0" CACHE PATH "The path to the GoogleMock test framework.") #Add a subdirectory to the build. The directory specified must contain a CMakeLists.txt file. # GMOCK_DIR value has been set with the set command # CMAKE_BINARY_DIR <-- The path to the top level of the build tree. That is the directory from where cmake is run. add_subdirectory(${GMOCK_DIR} {CMAKE_BINARY_DIR}/gmock) #Add the given directories to those the compiler uses to search for include files include_directories(SYSTEM ${GMOCK_DIR}/gtest/include ${GMOCK_DIR}/include) include_directories(${CMAKE_SOURCE_DIR}/test) #The add_executable command tells CMake to create a binary #The first argument is the name of the binary to create, the #rest are source files. Header files are not included in this #command. add_executable(testprojtest main.cpp) #target_link_libraries specifies libraries or flags to use #when linking a given target. The named target (first #argument) must have been created in the current directory #with add_executable() or add_library() target_link_libraries(testprojtest gmock_main)
- Create a new folder build by right clicking on TestProj and selecting New - Folder
- Now we will create a Make Target to generate the Makefiles from the CMakeLists.txt files.
- Select the Make Target tab on the right side of Eclipse
- Expand the TestProj project and right click on the build folder
- Click on New…
- Enter Target name:
cmake
- Uncheck Same as the target name and make the Make target box empty
- Uncheck Use builder settings and enter Build command:
cmake ..
(please note the space followed by two dots at the end) - Click on OK
- Double-click on the new Make Target cmake. Some files and folders shall be generated in the build folder. You can verify this in the Project Explorer on the left side of Eclipse.
- Now we will configure some TestProj project settings so that we can build
- Right click on the TestProj project in the Project Explorer and select Properties
- Go to “C/C++ Build”, select tab “Builder Settings” and click on Workspace… button
- Expand the folder tree and select the build folder. Click OK
- Click on Apply
- Click on OK to exit the project properties for TestProj
- Select menu Project - Build Project (click on TestProj in the Project Explorer first if this menu item is gray)
- Open the Console tab and check that the build went fine
- Select menu Run - Run Configurations…
- Select C/C++ Application on the left and click on the New button
- Enter Name: TestProjConsole
- Click on button Search Project… and select testprojtest. Click OK.
- You should now have build/test/testprojtest in the C/C++ Application field
- Click on Apply and then on Run
- You should get the following in the Console tab:
[==========] Running 0 tests from 0 test cases. [==========] 0 tests from 0 test cases ran. (0 ms total) [ PASSED ] 0 tests.
- This means that we have run all our Google Tests. However currently we have 0 tests in our project.
Create a test
We will create a test for a class called AddClass that can be used to add numbers.
- Create a new file in the test folder with name:
AddClassTest.cpp
- Add the following to the new file:
#include "gmock/gmock.h" #include "AddClass.h" TEST(AddClassTest, CanAddTwoNumbers) { //Setup AddClass myAddClass; //Exercise int sum = myAddClass.add(1,2); //Verify ASSERT_EQ(3, sum); }
- Locate the following line test/CMakeLists.txt:
add_executable(testprojtest main.cpp)
Add the new file
AddClassTest.cpp
to the line:add_executable(testprojtest main.cpp AddClassTest.cpp)
- Build project via Project - Build Project
- The build should fail because of the missing AddClass implementation
- Create a new folder in the TestProj project with name:
src
- Create a new file in the src folder with name
AddClass.h
and the following content:#ifndef ADDCLASS_H_ #define ADDCLASS_H_ class AddClass { public: int add(int arg1, int arg2); } #endif
- Create a new file in the src folder with name
AddClass.cpp
and the following content:#include "AddClass.h" int AddClass::add(int arg1, int arg2) { return arg1 + arg2; }
- Now we include the src folder when building
- Locate the following line in the top CMakeLists.txt file:
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
Add the following line below it:
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
- Create a CMakeLists.txt file in the src folder with the following content:
add_library(src AddClass.cpp)
- Add the following line to the top CMakeLists.txt file:
add_subdirectory(src)
- Finally we must make the new src library known to the test application by adding the following line at the end of test/CMakeLists.txt:
target_link_libraries(testprojtest src)
- Build and run project
- You should now get the following output in the Console tab:
[==========] Running 1 test from 1 test case. [----------] Global test environment set-up. [----------] 1 test from AddClassTest [ RUN ] AddClassTest.CanAddTwoNumbers [ OK ] AddClassTest.CanAddTwoNumbers (0 ms) [----------] 1 test from AddClassTest (1 ms total) [----------] Global test environment tear-down [==========] 1 test from 1 test case ran. (5 ms total) [ PASSED ] 1 test.