Post

Google Test (GTest)

Google Test (GTest)

Google Test (GTest)


Prerequisites

1
- C++

1. What is Google Test (GTest)?

Google Test (GTest) is a C++ testing framework that allows developers to write and run automated tests for their code.

It is widely used in industry to ensure:

  • correctness
  • reliability
  • maintainability
  • safe refactoring

Google Test is a C++ testing framework that provides structure, assertions, and automation for verifying code behavior.

2. Why Use Google Test?

You can test manually:

1
2
3
4
int result = add(2, 3);

if (result != 5)
    std::cout << "error\n";
❌ Problems
  • no structure
  • hard to scale
  • no automation
  • difficult debugging
  • cannot integrate with CI
✔ With Google Test
1
EXPECT_EQ(add(2, 3), 5);

You get:

  • automatic execution
  • structured results
  • failure diagnostics
  • CI/CD integration

3. Core Concepts

3-1. Test Suite & Test Case

1
TEST(AddTest, Basic)
  • AddTest → test suite (group)
  • Basic → test case

3-2. Assertions

Assertions verify results.

Common Assertions
1
2
3
4
EXPECT_EQ(a, b);   // equal
EXPECT_NE(a, b);   // not equal
EXPECT_TRUE(x);    // true
EXPECT_FALSE(x);   // false
Difference
TypeBehavior
EXPECT_*continues on failure
ASSERT_*stops immediately

Example

Code under test
1
2
3
4
int add(int a, int b)
{
    return a + b;
}
Test code
1
2
3
4
5
6
7
#include <gtest/gtest.h>

TEST(AddTest, Basic)
{
    EXPECT_EQ(add(2, 3), 5);
    EXPECT_EQ(add(-1, 1), 0);
}
Output
1
2
[ RUN      ] AddTest.Basic
[       OK ] AddTest.Basic

Realistic Example (Image Processing)

Code under test
1
2
3
4
5
6
7
8
9
10
struct Frame
{
    int width;
    int height;
};

Frame Resize(const Frame& input)
{
    return {input.width * 2, input.height * 2};
}
Test code
1
2
3
4
5
6
7
8
9
10
11
#include <gtest/gtest.h>

TEST(ResizeTest, DoubleSize)
{
    Frame input{100, 50};

    Frame output = Resize(input);

    EXPECT_EQ(output.width, 200);
    EXPECT_EQ(output.height, 100);
}

This verifies correctness of the algorithm

4. How Google Test Works

Build Flow
1
2
3
4
5
6
7
8
9
Your code
   ↓
Test code (calls your functions)
   ↓
Link with GTest
   ↓
Test executable
   ↓
Run tests
  • test is a separate executable
  • GTest provides the test runner

5. CMake Integration

CMakeLists.txt

1
2
3
4
5
6
7
cmake_minimum_required(VERSION 3.14)
project(MyProject)

find_package(GTest REQUIRED)

add_executable(test_app test.cpp)
target_link_libraries(test_app GTest::GTest GTest::Main)

Run

1
./test_app

Or:

1
ctest

6. Testing Patterns

✔ Test Multiple Cases
1
2
3
4
5
6
TEST(AddTest, VariousInputs)
{
    EXPECT_EQ(add(1, 1), 2);
    EXPECT_EQ(add(0, 0), 0);
    EXPECT_EQ(add(-1, -1), -2);
}
✔ Edge Cases
1
2
3
4
TEST(AddTest, EdgeCases)
{
    EXPECT_EQ(add(INT_MAX, 0), INT_MAX);
}
✔ Invalid Input Handling
1
2
3
4
5
6
7
8
9
TEST(ResizeTest, ZeroSize)
{
    Frame input{0, 0};

    Frame output = Resize(input);

    EXPECT_EQ(output.width, 0);
    EXPECT_EQ(output.height, 0);
}

7. Common Mistakes

❌ Only testing happy path

real bugs happen in edge cases

❌ No test isolation

tests affect each other

❌ Ignoring performance

critical in C++ systems

❌ Writing overly complex tests

tests should be simple and clear

8. Advantages

✔ Automation

Run tests automatically

✔ Clear reporting

Shows exact failure

✔ CI/CD integration

Works with pipelines

✔ Scalability

Handles large codebases

✔ Maintainability

Supports safe refactoring

9. Google Test vs Manual Testing

FeatureManualGTest
Automation
Scalability
Debugging
CI Integration
This post is licensed under CC BY 4.0 by the author.