0Pricing
C++ Academy · Lesson

Setting Up GTest

Add and run the framework.

Setting Up GTest is a free C++ Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why a Test Framework?

Google Test (GTest) is a widely used C++ unit-testing framework. It provides test registration, rich assertions, fixtures, and clear reporting so you can verify code automatically.

Getting GTest

Common ways to obtain GTest:

  • System package (e.g. libgtest-dev)
  • FetchContent in CMake pulling from GitHub
  • A vendored submodule

FetchContent is the most portable for new projects.

CMake with FetchContent

A minimal CMake setup downloads GoogleTest and links it.

# CMakeLists.txt
include(FetchContent)
FetchContent_Declare(
  googletest
  URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip
)
FetchContent_MakeAvailable(googletest)

add_executable(my_tests test_main.cpp)
target_link_libraries(my_tests GTest::gtest_main)

Your First Test

A test is declared with the TEST macro taking a test-suite name and a test name. Inside, you use assertions.

#include <gtest/gtest.h>

int add(int a, int b) { return a + b; }

TEST(MathTest, AddsPositive) {
    EXPECT_EQ(add(2, 3), 5);
}

No main() Needed

Linking against GTest::gtest_main supplies the main() that runs all registered tests. You only write the test cases.

Writing Your Own main

If you need a custom entry point, initialize GTest yourself.

#include <gtest/gtest.h>

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

Building and Running

After configuring CMake, build and run.

  • cmake -S . -B build
  • cmake --build build
  • ./build/my_tests

GTest prints a colored pass/fail summary.

Reading the Output

Each test reports [ RUN ] then [ OK ] or [ FAILED ]. A final line shows how many tests passed. Failures include the file, line, and expected vs actual values.

Running a Subset

Use the --gtest_filter flag to run only matching tests, e.g. ./my_tests --gtest_filter=MathTest.*. This speeds up focused debugging.

Integrating with CTest

Call enable_testing() and register the binary with gtest_discover_tests so ctest can run your suite in CI.

# CMakeLists.txt
enable_testing()
include(GoogleTest)
gtest_discover_tests(my_tests)

A Healthy Workflow

Write a failing test, implement the code, watch it pass. Keep tests fast and independent so you can run them on every change.

Quick Check

Recall how main() is provided.

Recap

You set up Google Test.

  • Obtain via FetchContent, package, or submodule
  • TEST(Suite, Name) declares a test
  • Link gtest_main for an automatic main()
  • Build, run, filter, and integrate with CTest

Frequently asked questions

Is the “Setting Up GTest” lesson free?

Yes — the full text of “Setting Up GTest” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Setting Up GTest”?

Add and run the framework. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Setting Up GTest” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Setting Up GTest
  2. Assertions and Matchers
  3. Fixtures and Setup
  4. Mocking with GMock
← Back to C++ Academy