Aug 29

Gtest – Google Test – Cross platform test framework

:Setting up the gtest Windows project



Theoretical Background:


Gtest is a cross platform framework for writing tests in c++

It supports Android, Windows, Mac, IOS and more.


gtest main page:



Google Test Runner:


Is a Visual Studio extension that allows to run gtest from the VS built in Test Explorer – All the tests that you write are automatically identifies by VS and can be run from the Text Explorer window as if they were VSTest / MSTest.


In order to test the c++ code for android, we need that the same test code will be compiled by the Android NDK with the compiler defined there and it needs to be run on a device or an emulator using the ADB.


Test Structure:

We can create a single stand-alone test without any relation to a class with a TearDown or a SetUp method (Will be explained later on in this article). Instead of a name of the class we will choose a name that will represent the Test-Case that our test relates to.




We can group together more than one test to a Test-Case and even relate this Test-Case to a class with a TearDown and a SetUp method (usually these Test-Cases are grouped logically by the module each Test-Case tests)


   namespace Test
        class Lior_Test : public ::testing::Test 
            virtual void SetUp() override
                //Setup code that will be run before each test

            virtual void TearDown() override
                //TearDown code that will be run after each test

        TEST_F(Lior_Test, TestLior1)
            //The actual test


The class needs to inherite from::testing::Test and then we will be able to override the SetUp and TearDown method.

The SetUp method will be called before each test is run.

The TearDown method will be called after each test is run.

We will define the test as:


TEST_F(Lior_Test, TestLior1)


Lior_Test is the name of the class that we created and TestLior1 is the name of the test.

We can group together more than one Test to each TestCase and we can write helper method (static or not) that can be called by every test.


In order to set a result for the Test:

We will use the ASSERT and EXPECT method, which are a part of the gtest API.

When an ASSERT command fails, it fails the entire test and stops it.

When an EXPECT command fails, it fails the test but the test will continue to run after the failure.


Example for an ASSERT command:


EXPECT_TRUE(i == 1) << "i does not equal 1";


There are more Macros in gtest that makes ASSERT simpler, for example:



We can also check if a specific exception was thrown or not, for example:


ASSERT_THROW(MethodExpectedToThrow(), ExceptionExceptedToBeThrown);


In conclusion:


I found gtest very simple to use and it provides (along with gmock) enough functionality to meet my c++ test needs.


Good Luck !

Leave a Reply

Your email address will not be published.

אתם יכולים להשתמש באפשרויות ותגי ה-HTMLהבאים: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>