Unit test in C++ with Google Test

I had the extreme need of building a C/C++ CGI this weekend and, why not, i wanted it fully covered by tests.

As always, for any need, Google has a solution and, in this case, for my testing needs, Google had Google Test (gtest), a pretty easy to use library that allows you to write test cases (made of assertions) and compile/run them to check the project’s integrity.

Gtest is farily easy to use and has close to no requirements, i’ve run it in OSX and Ubuntu Server withouth any problem.

Installation

Just grab the repository and cd into the googletest folder, once there:

$ cmake .
$ make
# make install

and you are done.

Compilation

GTest is compiled as a library, so building it is as simple as using pthreads or sdl. Once you have your tests written, you will need an entry point (just like any regular project), the entry point must be “main” as shown in the Testing section.

Once the test program is ready, compile it using:

g++ INPUT -o OUTPUT -lgtest

GTest basics

As we said before, gtest is based on assertions, a test will check if some “cases” fails, in this case, the test will fail, otherwise, the test will be successful.

Gtest has 2 different assertions (ASSERT and EXPECT) so we can decide if an error is fatal or not. In order to create a test, we will check if the output is what we expected it to be, if it is, we will consider the test as a success.

A test case contains one or many test functions, each of these functions checks the behaivour of one or some features, this will be grouped later into a test program. This structure allows us to run the whole test program, or just some cases or functions.

Assertions

An assertion in gtest is a macro that checks if something is what it’s expected to be or not, for example if we checks if 1 == 2 with ASSERT_EQ(1,2) it’ll fail and our test case will output an error.

Gtest has different assertions that can be found in it’s documentation.

Testing

So, now we know what gtest is and how it works, lets code some tests.
Gtest has 2 different test macros, the first one (and the simplest one), is TEST().
TEST() allows us to check certain things that doesn’t need any preset. It takes two params, the test case name, and it’s own identificator, so, for example, if we want to perform the same test as before (1==2) we would write it like this:

#include "gtest/gtest.h"
TEST(equals, one_is_two) {
ASSERT_EQ(1, 2);
}

But sometimes we need more control, we need some setup or things to share between cases and TEST is not enough, to suit this needs, gtest has yet another test statement, TEST_F(). TEST_F() lets us to define a class to feed all the tests, so we can have persistency, setups before each case and much more cool stuff.

Running

What now? just run it.

#include "gtest/gtest.h"

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

The ::testing::InitGoogleTest function normalizes the arguments.

The RUN_ALL_TESTS() macro is self explainatory and will run our cases.

Once you have the tests written and the main function with the gtest’s entry point, you are ready to build using the gtest library (-lgtest) and run the executable to check your projects health.

Easy, right?

Maybe some examples would clarify it even more, feel free to have a peek in my project’s gtest implementation, it can be found here.
Hope it helped.

Leave a comment