-
Notifications
You must be signed in to change notification settings - Fork 8
Setting Up Google Test (gtest) and Google Mock (gmock)
These are notes taken during Hannah's presentation of gtest & gmock:
-
Install MinGW. Ensure it is in your PATH
-
Install CMake (you can use the .msi installer). Make sure you set the option to add it to your path
-
Clone the Google Test respository
-
In the command prompt, navigate to the location where you put the Google Test respository
-
Create a build folder ("mkdir build") and change directory to it ("cd build")
-
Run this command: cmake ../ -G “Eclipse CDT4 – MinGW Makefiles"
-
In the make folder, run this: mingw32-make
-
Open Eclipse, and under Help, select Install New Software. Choose to "Work with" Neon
-
Filter for C/C++ Unit Testing Support and install the package there
-
Create a new project (for example, Hello World). Under executables, choose MinGW as the toolchain
-
Right-click your project, and go to Properties -> C/C++ General -> Paths and Symbols
-
In Includes, go to GNU C++ and add the path to googletest/googletest/include
-
Under libraries, add "gtest" and "gtest_main"
-
Go to Library Paths and add the path to googletest/build/googlemock/gtest
Now you should be able to use Google Test. Example:
#include <iostream>
#include <gtest/gtest.h>
using namespace std;
TEST(abcTestCase, canAssertTrue){
// First argument is a "test case", which groups the tests
// Second argument describes what the test is about
// -- THESE CANNOT HAVE AN UNDERSCORE IN THEM --
ASSERT_TRUE(true);
}
- Now go to Run -> Run Configurations...
- Go to C/C++ Unit, and click the New launch configuration icon
- In the new launch configuration, go to the C/C++ Testing tab and select Google Tests Runner as the tests runner
- Click run
Any class whose members are to be mocked need to have those members declared as virtual or abstract. This is because the Google Mock framework must be allowed to implement them in its own fashion.
To use Google Mock, you need to do the following:
- Right-click your project, and go to Properties -> C/C++ General -> Paths and Symbols
- In Includes, go to GNU C++ and add the path to googletest/googlemock/include
- Under libraries, add gtest, gmock, and gmock_main
- Go to Library Paths and add the path to googletest/build/googlemock/gtest and googletest/build/googlemock