It’s always good to unit test your C++ code so that you can find problems early. In this tutorial, I will demonstrate how you can run unit testing by using of Google C++ Testing Framework using the Microsoft Visual Studio. GTest is easy to learn and master. Visit the following website to find out more about GTest Google C++ Testing Framework

Note: I am using VS2010 C++ Express Edition in this tutorial.

Compile the GTest framework

  1. Download GTest framework from http://code.google.com/p/googletest/downloads/list. You will find the ZIP file listed in the download page. The version that i am using in this tutorial is gtest-1.6.0.
  2. Unzip the file to C:\gtest-1.6.0\
  3. Browse to msvc\ folder. All files are set to read-only by default. Uncheck read-only attribute in the properties setting for all files so that it allow to write.
  4. Open the gtest-md.sln file using visual studio.
  5. Compile the solution in release mode.
    Note: Debug build will generate gtestd.lib; Release build will generate gtest.lib
  6. Create a lib folder in C:\gtest-1.6.0\
  7. Copy gtest.lib from msvc\gtest-md\Release\ to C:\gtest-1.6.0\lib\ C++ Unit Testing with Google C++ Testing Framework

Unit Test with GTest in the C++ Example

  1. Create a new C++ win32 console application. I called this project GTestSample.
  2. Right click the GTestSample project and select properties.
  3. Enter the Gtest include folder path to C/C++::General::Additional Include Directories field.C++ Unit Testing with Google C++ Testing Framework

  4. Enter the Gtest lib folder path to Linker::General::Additional Library Directories field.C++ Unit Testing with Google C++ Testing Framework
  5. We now start the coding part by insert the following header to the cpp file.
    1
    #include <gtest/gtest.h>
  6. Let’s write the first function and its test cases. It’s a multiply function that each given number will return by multiply of 3.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    int MultiplyThree(int num)
    {
      return num * 3;
    }

    TEST(MultiplyThreeTest, InputNumber) {
      EXPECT_EQ(6, MultiplyThree(2)); // Expect equal to 6
      EXPECT_EQ(9, MultiplyThree(3)); // Expect equal to 9
      EXPECT_EQ(12, MultiplyThree(4)); // Expect equal to 12
      EXPECT_NE(20, MultiplyThree(5)); // Expect not equal to 20
      EXPECT_NE(25, MultiplyThree(6)); // Expect not equal to 25
    }
  7. Our next function and its test cases will compare the char string.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    int CompareChar(char* ch1, char* ch2)
    {
      if (ch1 == NULL || ch2 == NULL)
        return 88;

      return strcmp(ch1, ch2);
    }

    TEST(CompareCharTest, InputChar) {

      // Expect equal
      EXPECT_EQ(0, CompareChar("hello", "hello"));

      // Expect not equal
      EXPECT_NE(0, CompareChar("hello", "world"));

      // Expect greater than. "world" is grater than "hello"
      EXPECT_GT(0, CompareChar("hello", "world"));

      // Expect less than. "hello" is less than "world"
      EXPECT_LT(0, CompareChar("world", "hello"));

      // Expect equal.
      // Either char string appear to be NULL, return 88
      EXPECT_EQ(88, CompareChar(NULL, "world"));
    }
  8. Let’s create a class now and test it with GTest. A sample class that compare its value.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    class SampleClass
    {
    public:
      SampleClass(int value)
      {
        m_value = value;
      }

      int CompareValue(int val)
      {
        return (m_value == val);
      }


    private:
      int m_value;
    };

    TEST(SampleClassTest, InputNumber) {
      SampleClass sample(10);
      EXPECT_EQ(1, sample.CompareValue(10)); // Expect equal
      EXPECT_EQ(0, sample.CompareValue(8)); // Expect equal
      EXPECT_NE(1, sample.CompareValue(8)); // Expect not equal
    }
  9. Let’s make sure that we run all the test cases in the main function.
    1
    2
    3
    4
    5
    6
    7
    int _tmain(int argc, _TCHAR* argv[])
    {
      ::testing::InitGoogleTest(&argc, argv);
      int i = RUN_ALL_TESTS();
      getchar();
      return i;
    }

Note: It’s important that you need to make sure that you build with the correct GTest library. In this tutorial, our GTest library was built in release mode, so we will compile the C++ example in release mode too.

Compile and run the C++ program now and you should observe the following output: C++ Unit Testing with Google C++ Testing Framework

Download source code: GTestSample.zip