Creating Google tests

Before we start setting up Google Test, let's download Google C++ Testing Framework from the GitHub link here: https://github.com/google/googletest. Click on the Clone or download button and then Download ZIP:

Once you have all the required files, unzip the files to a directory on your PC and follow these steps to create a test project:

  1. Create a new project by going to File | New File or Project.
  2. Select Auto Test Project under the Other Project category.
  3. In the Project and Test Information dialog, pick Google Test for the Test framework option.
  4. After that, fill in the Test suite name and Test case name fields.
  5. Check the Enable C++ 11 checkbox if you want to support C++ 11 features in the test.
  6. As for the Google test repository field, select the directory where you unzipped the file you just downloaded from GitHub—for example, C:\googletest-master.
  7. Select your Build system. Leave it as qmake if you are not sure of what to do. You only need to change this option if you are using certain other build systems such as CMake or Qbs.
  8. Press Next and complete the rest of the process.

Once the project has been created, you will see that several things have been set up for you by the project wizard we just walked through. If you open up gtest_dependency.pri, you can see that the settings for INCLUDEPATH, SOURCES, and so on have all been set for you. The actual source file that contains the test functions is located at tst_testscene.h, which looks something like this:

#ifndef TST_TESTCASE_H
#define TST_TESTCASE_H

#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>

using namespace testing;

TEST(TestSuite, TestCase)
{
EXPECT_EQ(1, 1);
ASSERT_THAT(0, Eq(0));
}

#endif // TST_TESTCASE_H

Similar to Qt Test, Google Test also uses macros such as EXPECT_EQ and ASSERT_THAT to do the test. This is what they do:

If you build and run the project now, you should see a similar result to this:

To learn more about the other macros available in Google C++ Testing Framework, visit https://github.com/google/googletest/blob/master/googletest/docs/primer.md.

Now, let's see how Boost tests work.