Google C++ Testing Framework(Google Test / GTest) 


INTRODUCTION

Google’s framework for writing C++ tests on a variety of platforms(Linux Mac OS X, Windows).

It supports automatic test discovery, a rich set of assertions, user-defined assertions, death tests, fatal test and non-fatal failures, value and type – parameterised tests, various options for running the tests, and XML test report generation.

Why GTest framework ?

1. When GTest fails, GTest allows you to run it in isolation for quick debugging.

2. GTest groups related tests into test cases that can share data and subroutines.

3. GTest works on different OSs with different compilers , with or without exceptions.

( Note: Here we discussing GTest on Linux. )

How GTest Works ?

When using GTest, you start by writing assertions, which are statements that check whether a condition is true. Tests use assertions to verify the tested code’s behavior. If a test crashes or has a failed assertion, then it fails; otherwise it succeeds.

ASSERTIONS

GTest assertions are macros that resemble function calls. You test a class or function by making assertions about its behavior. When an assertion fails, GTest prints the assertion’s source file and line number location, along with a failure message.

ASSERT_* versions generate fatal failures when they fail, and abort the current function.

EXPECT_* versions generate nonfatal failures, which don’t abort the current function.

Usually, EXPECT_* are preferred, as they allow more than one failures to be reported in a test. However, you should use ASSERT_*  if it doesn’t make sense to continue when the assertion in question fails.

1. Basic Assertions

These assertions do basic true/false condition testing.

Fatal Assertion Non-fatal Assertion Verifies
ASSERT_TRUE(condition); EXPECT_TRUE(condition); condition is true
ASSERT_FALSE(condition); EXPECT_FALSE(condition); condition is false

2. Binary Comparison

These assertions compare two values.

Fatal Assertion Non-fatal Assertion Verifies
ASSERT_EQ(expected,actual); EXPECT_EQ(expected,actual); expected==actual
ASSERT_NE(val1, val2); EXPECT_NE(val1, val2); val1!=val2
ASSERT_LT(val1,val2); EXPECT_LT(val1,val2); val1<val2
ASSERT_LE(val1,val2); EXPECT_LE(val1, val2); val1=<val2
ASSERT_GT(val1,val2); EXPECT_GT(val1, val2); val1>val2
ASSERT_GE(val1,val2); EXPECT_GE(val1, val2); val1>=val2

3. String Comparison

These assertions compare two C strings. If you want to compare two string objects, use EXPECT_EQEXPECT_NE, and etc instead.

Fatal Assertion Non-fatal Assertion Verifies
ASSERT_STREQ(expected_str,actual_str); EXPECT_STREQ(expected_str,actual_str); the two C strings havethe same content
ASSERT_STRNE(str1,str2); EXPECT_STRNE(str1,str2); the two C strings havedifferent content
ASSERT_STRCASEEQ(expected_str,actual_str); EXPECT_STRCASEEQ(expected_str,actual_str); the two C strings havethe same content, ignoring case
ASSERT_STRCASENE(str1,str2); EXPECT_STRCASENE(str1,str2); the two C strings havedifferent content, ignoring case

Download GTest

To download GTest, click here.

Linux Requirements

1. GNU  make

               $ sudo apt-get install make

2. POSIX standard shell

              $ sudo apt-get install bash

3. C++ compiler

             $ sudo apt-get install g++

Installation Steps

1. Decompress

            $ unzip gtest-1.6.0.zip

2. Go to this directory.

            $ cd gtest-1.6.0/

3. Standard GNU configure script

            $ ./configure

4. Standard makefile

            $ make

3. Builds and run all tests

           $ make check

Build Sample Test

As an example, the make/ directory contains a Makefile that you can use to build Google Test on systems (e.g. Linux, Mac OS X). It just builds the Google Test library and a sample test.

           $ cd make

           $ make

          $ ./sample1_unittest

If you are getting errors while running make, then open Makefile with sudo power

           $ sudo gedit Makefile

and replace “-lpthread” with “-pthread” at line number 80.

Build GTest samples

Jointly with the GTest, it also comes some C++ unit test examples, found in the samples directory. You can build this examples using CMake. It can be installed :

            $ sudo apt-get install cmake

The CMake uses a configuration file named CMakeLists.txt. To build a project with CMake is create a build directory, generate a Makefile using CMake and build it with make.

            $ mkdir build

            $ cd build

          $ cmake –Dgtest_build_samples=ON ..

           $ make

One advantage of using CMake is that you separate the deploy from source code and can make the deploy in multiple places with the same CMake file.

Now, execute all examples.

             $ ./sample1_unittest

             $ ./sample2_unittest

                   . . . 

            $ ./sample10_unittest

Build a simple example

1. First, build the GTest Library.

               $ g++ -I. -I./include -c src/gtest-all.cc

               $ ar -rv libgtest.a gtest-all.o

It will generates the libgtest.a.

2. Consider a simple unit test example of a C source code named mytest.c:

             #include <gtest/gtest.h>

            TEST(MathTest, Addition)

             { 

                        EXPECT_EQ(2 + 2, 4);

             }

             int main(int argc, char **argv)

             {

                    ::testing::InitGoogleTest( &argc, argv );

                    return RUN_ALL_TESTS();

             }

To build it, it is necessary to defines the GTest headers directory (parameter includes dir -I), compile the source code and link it with the GTest library (libgtest.a) and pthread :

               $ g++ -I. -I./include mytest.c libgtest.a -lpthread -o mytest

3. Now, call

               $ ./mytest

Output is :

mytest

Learn more.

Advertisements

4 thoughts on “Google C++ Testing Framework(Google Test / GTest) 

  1. Very practical post about gtest.
    Thank you very much!

    only the following:
    $ g++ -I. -I./include mytestest.c libgtest.a -lpthread -o mytest

    should be:
    $ g++ -I. -I./include mytest.c libgtest.a -lpthread -o mytest

  2. Pingback: gtest – weicode

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s