How to use C++ unit testing with GoogleTest

Before moving to the GoogleTest library’s basics, let’s quickly understand what unit testing is and why it is essential. Unit testing plays an essential role in the software development process, as it enables developers to detect and resolve bugs, validate code modifications, and ensure the expected functionality of individual code units.

GoogleTest (also known as gtest) is a unit test library for C++. The GoogleTest library provides a lot of functionalities. We will discuss GoogleTest assert functions. So, to run these tests in C++, we need to include the #include <gtest/gtest.h> library.

Let’s see an example of ASSERT_TRUE and ASSERT_FALSE in C++ below:

#include <gtest/gtest.h>
bool IsEven(int num) {
return num % 2 == 0;
}
TEST(TestingCode, IsEvenTest) {
ASSERT_TRUE(IsEven(4)); // Assertion succeeds since 4 is even
ASSERT_FALSE(IsEven(7)); // Assertion succeeds since 7 is not even
ASSERT_TRUE(IsEven(5)); // Assertion fails since 5 is not even
}
int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

Explanation

  • Line 1: We include the necessary header file gtest.h, which contains the GoogleTest framework’s functionality.

  • Lines 3–5: The function IsEven takes an integer num as a parameter and checks if it is even. It returns true if the number is even and false otherwise.

  • Lines 7–10: This block defines a test case named IsEvenTest inside the TestingCode test suite. It contains three assertions using the ASSERT_TRUE and ASSERT_FALSE macros provided by GoogleTest.

    • The first assertion checks if IsEven(4) returns true. Since 4 is even, the assertion succeeds.
    • The second assertion checks if IsEven(7) returns false. Since 7 is not even, the assertion succeeds.
    • The third assertion checks if IsEven(5) returns true. Since 5 is not even, the assertion fails.

Below is an example related to EXPECT_EQ and EXPECT_NE.

#include <gtest/gtest.h>
TEST(TestingCode, IsEqual){
//case 1
int x = 5;
int y = 5;
EXPECT_EQ(x, y); // Expectation pass, as x is equal to y
//case 2
int a = 10;
int b = 0;
EXPECT_NE(a, b); //// Expectation passes, as 10 is not equal to 0
// case 3
int* ptr1 = nullptr;
int* ptr2 = new int(10);
EXPECT_NE(ptr1, ptr2); // Expectation passes, as the pointers are not equal
}
int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

Explanation

  • Line 1: We include the necessary header file gtest.h, which contains the GoogleTest framework’s functionality.

  • Line 7: This line is an assertion that checks if the x and y values are equal. The EXPECT_EQ macro compares the values and passes the test if they are equal. In this case, the expectation passes since x and y are both 5.

  • Line 11: This line is an assertion that checks if the values of a and b are not equal. The EXPECT_NE macro compares the values and passes the test if they are not equal. In this case, since a is 10 and b is 0, the expectation passes.

  • Line 15: This line is an assertion that checks if the values of ptr1 and ptr2 (i.e., the addresses they point to) are not equal.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved