Skip to content
This repository has been archived by the owner on Sep 27, 2021. It is now read-only.

Setting Up Google Test (gtest) and Google Mock (gmock)

Tyler Gamvrelis edited this page Oct 30, 2018 · 3 revisions

These are notes taken during Hannah's presentation of gtest & gmock:

Google Test

  1. Install MinGW. Ensure it is in your PATH

  2. Install CMake (you can use the .msi installer). Make sure you set the option to add it to your path

  3. Clone the Google Test respository

  4. In the command prompt, navigate to the location where you put the Google Test respository

  5. Create a build folder ("mkdir build") and change directory to it ("cd build")

  6. Run this command: cmake ../ -G “Eclipse CDT4 – MinGW Makefiles"

  7. In the make folder, run this: mingw32-make

  8. Open Eclipse, and under Help, select Install New Software. Choose to "Work with" Neon

  9. Filter for C/C++ Unit Testing Support and install the package there

  10. Create a new project (for example, Hello World). Under executables, choose MinGW as the toolchain

  11. Right-click your project, and go to Properties -> C/C++ General -> Paths and Symbols

  12. In Includes, go to GNU C++ and add the path to googletest/googletest/include

  13. Under libraries, add "gtest" and "gtest_main"

  14. 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);
}
  1. Now go to Run -> Run Configurations...
  2. Go to C/C++ Unit, and click the New launch configuration icon
  3. In the new launch configuration, go to the C/C++ Testing tab and select Google Tests Runner as the tests runner
  4. Click run

Google Mock

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:

  1. Right-click your project, and go to Properties -> C/C++ General -> Paths and Symbols
  2. In Includes, go to GNU C++ and add the path to googletest/googlemock/include
  3. Under libraries, add gtest, gmock, and gmock_main
  4. Go to Library Paths and add the path to googletest/build/googlemock/gtest and googletest/build/googlemock