ReviseAlgo Logo

Testing in C++

Google Test Fundamentals

Write unit tests in C++ using the Google Test (GTest) framework and manage assertions.

Interview: Differentiating between EXPECT_ and ASSERT_ assertions, and using test fixtures to share setup/cleanup logic.

Last Updated: June 13, 2026 9 min read

Google Test (GTest) is the industry-standard framework for writing unit tests in C++. It provides assertions, test fixtures, and custom test runners to verify code correctness.

Assertions

Verify conditions using GTest macros. Choose between non-fatal (EXPECT_) and fatal (ASSERT_) checks.

EXPECT vs ASSERT

EXPECT_EQ reports failure and continues the test. ASSERT_EQ aborts the test case immediately.

Test Fixtures

Inherit from testing::Test and override SetUp() and TearDown() to share initialization code.

GTest Assertion Classification

GTest provides double assertion tracks for checking values:

Non-Fatal Assertion Fatal Assertion Verification Purpose
EXPECT_EQ(a, b) ASSERT_EQ(a, b) Verify two values are equal (e.g. return values).
EXPECT_NE(a, b) ASSERT_NE(a, b) Verify two values are not equal.
EXPECT_TRUE(cond) ASSERT_TRUE(cond) Verify boolean condition is true.
EXPECT_THROW(op, ExType) ASSERT_THROW(op, ExType) Verify operation throws a specific exception type.

Code Walkthrough

A simple unit test suite using assertions and a sharing test fixture.

#include <gtest/gtest.h>
#include <vector>

// Code under test int add(int a, int b) { return a + b; }

// Test Fixture class VectorTest : public ::testing::Test { protected: std::vector<int> testVector;

void SetUp() override { testVector = {1, 2, 3}; // Setup initial state }

void TearDown() override { testVector.clear(); // Cleanup state } };

// 1. Basic Unit Test TEST(MathTest, Addition) { EXPECT_EQ(add(2, 3), 5); // Non-fatal assertion EXPECT_NE(add(2, 3), 6); }

// 2. Fixture-based Unit Test TEST_F(VectorTest, VectorSize) { ASSERT_EQ(testVector.size(), 3); // Fatal check: if this fails, subsequent reads crash EXPECT_EQ(testVector.at(0), 1); }

int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }

Interview-Relevant Information

Q: When should you use EXPECT_EQ vs. ASSERT_EQ?
Answer: Use EXPECT_EQ for general assertions where a failure does not block the rest of the test case. Use ASSERT_EQ when the check is critical for subsequent execution (e.g. verifying a pointer is not null before dereferencing it), as continuing would result in a crash.

Q: How do you share setup logic across tests in GTest?
Answer: Inherit your test class from ::testing::Test, define your setup code inside the overridden SetUp() method, and declare your tests using the TEST_F() macro. GTest instantiates a new fixture object for each test case, guaranteeing test isolation.

Quick Checklist

Did you use TEST_F when using test fixtures? Do you use ASSERT_ to check pointers before dereferencing them? If yes, your test suite is safe and correct.

Use Cases

Verifying core algorithm libraries and data structures before release.

Developing test suites to verify system functionality during refactoring.

Common Mistakes

Using EXPECT_ checks for pointer verification, leading to segmentation faults if a pointer is null.

Using ASSERT_ macros inside helper functions called by tests, which can result in incomplete test execution.