Google Test Installation Guide for C++ in Windows ( for Visual Studio Code)

Anusree S
The Startup
Published in
3 min readAug 18, 2020

--

Hey, budding c++ coders out there! On your way to becoming a fleshed out developer? I presume you have installed a code editor of your choice, installed compilers, debuggers and all the necessary libraries. But you are missing one mostly overlooked component by beginners, the testing tool.

We will see :

1. What is Google C++ testing framework?

2. Why Google test?

3. Installation steps

4. Sample program

1. What is Google C++ testing framework?

Google Test is a unit testing library for the C++ programming language, based on the xUnit architecture.

Or simply put, it makes your C++ testing easy and efficient. Basically, you’ll write a test program, containing many test cases and will check it against your program after compiling Google test into a library.

The test function is made up of macros, which are more like functions whose parameters are the values inputted to test the function. These macros are assertions which will return True or False according to the assertion type.

2. Why Google test?

It is portable and reusable across different platforms. It doesn’t terminate if one test case fails. But it will halt that particular test case and will continue testing other cases. There are 2 kinds of assertions provided by it: i) fatal ii) non-fatal. Moreover, related tests can be grouped into different cases. And each test is run on different objects.

3. Installation steps

  1. This Github repo contains libraries for google test. You can download the version of your choice.
  2. Download libgtest.a and libgtest_main.a libraries.
  3. Copy both these files into lib of MingW (Ex : C:\Program Files\mingw-w64\x86_64–8.1.0-win32-seh-rt_v6-rev0\mingw64\lib)
  4. Go to Google test downloaded repo, extract it and navigate to: googletest →include →gtest [ex C:\Users\Downloads\googletest-release-1.10.0\googletest-release-1.10.0\googletest\include\gtest]. Copy that whole gtest file and copy to the folder MingW\lib\gcc\x86_64-w64-mingw32\8.1.0\include.

You are all set to code!

4. Sample program

Open your Vs Code and let’s start coding. We will need 2 files, one, containing the function which is to be tested and another the test file itself.

triangle.h file

Here I first created a header file with the function declaration. Then I created another file, triangle.cpp with the function definition.

triangle.cpp

This is a simple function which 1,2 & 3 respectively for an equilateral, isosceles and scalene triangle.

triangle_test.cpp

Next is the process of creating the test program, triangle_test. Then we will compile both these files.

>> g++ triangle.cpp -c
>> g++ triangle_test.cpp -c
>> g++ triangle.o traingle_test.o -lgtest -lgtest_main -lpthread

Run the executable file at the end and here you go!

PS: If you are a beginner who is into NLP, please give this a read! Check out my other blogs too..

--

--