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 evenASSERT_FALSE(IsEven(7)); // Assertion succeeds since 7 is not evenASSERT_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
IsEventakes an integernumas 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
IsEvenTestinside theTestingCodetest suite. It contains three assertions using theASSERT_TRUEandASSERT_FALSEmacros provided by GoogleTest.- The first assertion checks if
IsEven(4)returns true. Since4is even, the assertion succeeds. - The second assertion checks if
IsEven(7)returns false. Since7is not even, the assertion succeeds. - The third assertion checks if
IsEven(5)returns true. Since5is not even, the assertion fails.
- The first assertion checks if
Below is an example related to EXPECT_EQ and EXPECT_NE.
#include <gtest/gtest.h>TEST(TestingCode, IsEqual){//case 1int x = 5;int y = 5;EXPECT_EQ(x, y); // Expectation pass, as x is equal to y//case 2int a = 10;int b = 0;EXPECT_NE(a, b); //// Expectation passes, as 10 is not equal to 0// case 3int* 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
xandyvalues are equal. TheEXPECT_EQmacro compares the values and passes the test if they are equal. In this case, the expectation passes sincexandyare both5. -
Line 11: This line is an assertion that checks if the values of
aandbare not equal. TheEXPECT_NEmacro compares the values and passes the test if they are not equal. In this case, sinceais10andbis0, the expectation passes. -
Line 15: This line is an assertion that checks if the values of
ptr1andptr2(i.e., the addresses they point to) are not equal.
Free Resources