Lets continue our C++ Unit testing talks.

Googletest installation for linux can be found here https://code.google.com/p/tonatiuh/wiki/InstallingGoogleTestForLinux, installing for windows or mac is out of the scope of this post.

  1. First download google test source from https://code.google.com/p/googletest/downloads/list
  2. The latest version is 1.7.0 for googletest
  3. You need python and cmake for installing googletest
  4. If you dont have python please install python to your system
  5. If you dont have CMake please install CMake on your system
  6. Extract googletest source code
    tar -xzvf gtest-version.tar.gz
  7. For build the library please type
    cd gtest-version
    mkdir mybuild
    cd mybuild
    cmake -G"Unix Makefiles" ..
  8. Now to install builded library type
    cp -r ../include/gtest /usr/local/include
    cp lib*.a /usr/local/lib

Before we go any further please check gtest samples folder, unit tests are well documented. Lets check one sample, we are talking about test first development and we must check unit test first.



#include "gtest/gtest.h"
#include "sample4.h"

// Tests the Increment() method.
TEST(Counter, Increment) {
  Counter c;

  // EXPECT_EQ() evaluates its arguments exactly once, so they
  // can have side effects.

  EXPECT_EQ(0, c.Increment());
  EXPECT_EQ(1, c.Increment());
  EXPECT_EQ(2, c.Increment());

First of all every unit googletest has gtest/gtest.h header and then your header file which you want to test. I am just talking about C++ unit tests, so I am not going to cover C unit testing. You will find it easy to code.

TEST macro gets two parameters, the first one is your test group ( I use CounterTest instead of using Counter like in this example ) and the second parameter is your test's name.

Here you see EXPECT_EQ macro that also gets two parameters. If you are familiar with unit testing, it is obvious that you dont need any information for this macro. The macro expects both parameters are equal and fails when these parameters are not equal.




// A simple monotonic counter.
class Counter {
  int counter_;

  // Creates a counter that starts at 0.
  Counter() : counter_(0) {}

  // Returns the current counter value, and increments it.
  int Increment();

  // Prints the current counter value to STDOUT.
  void Print() const;




#include "sample4.h"

// Returns the current counter value, and increments it.
int Counter::Increment() {
  return counter_++;

// Prints the current counter value to STDOUT.
void Counter::Print() const {
  printf("%d", counter_);

Possible errors while installing Googletest on Ubuntu

“CMAKE_CXX_COMPILER-NOTFOUND” was not found, please install build-essentials

It means that you don’t have the build tools installed (the g++ in this case).
Open a terminal window and run:

sudo apt-get install build-essential -y

Install Googletest on Netbeans

Please watch this video for adding Googletest support to your Netbeans.


Googletest Gbar

Google Test is an excellent xUnit style c++ unit testing framework. It is highly recommended. One (minor) drawback of Google Test is it’s text based UI, and this project attempts to help. You need mono-runtime for bilding this project.

This post is introduction to Googletest, we will continue talking about Googletest.