Show Menu
Cheatography

C++ (Gtest) Cheat Sheet (DRAFT) by

A cheat sheet on the Google test library.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

::test­ing­::Test — GTest Fixture

class my_fixture : public ::testing::Test {
protected:
    // shared member variables
    int shared_variable;
    
    void SetUp() override {
        shared_variable = 42;  // runs before each test
    }
    
    void TearDown() override {
        // cleanup after each test
    }
};
Base class for Google Test fixtures, providing automatic setup and teardown for each test.

Inherit from it to create a fixture that shares member variables and helper functions across multiple tests.

SetUp() runs before each test, TearDown() runs after each test — both are automa­tically called by Google Test.

::test­ing­::U­nit­Tes­t::­cur­ren­t_t­est­_info()

const auto* info = ::testing::UnitTest::GetInstance()->current_test_info();

info->name();            // returns the test name as a string
info->test_suite_name(); // returns the test suite name
info->result();          // returns the test result
Returns a pointer to inform­ation about the currently running test.

Contains details like the test name, test suite name, and whether the test has failed.

Commonly used in SetUp() to get the test name for creating unique temporary files or direct­ories.

EXPECT­_TRUE

EXPECT_TRUE(condition);           // marks failed but continues running
ASSERT_TRUE(condition);           // stops test immediately if false
A Google Test macro that checks if a condition is true and marks the test as failed if it isn't.

Unlike ASSERT­_TRUE, the test continues running after a failure so other checks can still be evaluated.

Use when you want to collect multiple failures in one test run rather than stopping at the first one.

EXPECT_LT

EXPECT_LT(actual_value, upper_bound);

// common usage
EXPECT_LT(elapsed_time, std::chrono::seconds(5));  // verify operation was fast
EXPECT_LT(count, 10);                              // verify count is under 10
A Google Test macro that checks if the first value is less than the second, marking the test as failed if it isn't.
Unlike ASSERT_LT, the test continues running after a failure so other checks can still be evaluated.
Commonly used to verify something happened quickly or within a time limit.
 

::test­ing­::T­est­::S­etUp()

void SetUp() override {
    temp_directory = create_temp_dir();
    prepend_to_path(fake_bin_dir);
    saved_path = get_current_path();
}
A virtual function inherited from ::test­ing­::Test that runs automa­tically before each individual test.

Override it to initialise member variables, create temporary files, or prepare any state needed by the tests.

Always paired with TearDown() to clean up anything created here.

::test­ing­::U­nit­Tes­t::­Get­Ins­tance()

const auto* unit_test = ::testing::UnitTest::GetInstance();
const auto* test_info = unit_test->current_test_info();

// or chained together
const auto* test_info = ::testing::UnitTest::GetInstance()->current_test_info();
Returns a pointer to the single global instance of the UnitTest object.

Uses the Singleton pattern — only one instance ever exists, and this method is how you access it.

From it you can retrieve inform­ation about the currently running test suite and individual tests.

ASSERT­_TRUE

ASSERT_TRUE(condition);          // stops test immediately if false
EXPECT_TRUE(condition);          // marks failed but continues running
A Google Test macro that checks if a condition is true and immedi­ately aborts the test if it isn't.

Use when the rest of the test cannot meanin­gfully continue if this check fails.

Contrast with EXPECT­_TRUE which marks the test as failed but allows it to keep running.

EXPECT_EQ

EXPECT_EQ(actual_value, expected_value);
A Google Test macro that checks if two values are equal and marks the test as failed if they aren't.

Unlike ASSERT_EQ, the test continues running after a failure so other checks can still be evaluated.

Prints both the expected and actual values in the failure message making it easy to diagnose.
 

testin­g::­Tes­t::­Tea­rDown()

void TearDown() override {
    restore_path(saved_path);      // restore original PATH
    remove_temp_directory(tmpDir); // delete temporary files
}
A virtual function inherited from ::test­ing­::Test that runs automa­tically after each individual test.

Override it to clean up anything created in SetUp() — delete temporary files, restore enviro­nment variables, free resources.

Always runs even if the test fails, ensuring cleanup always happens.

TEST_F

TEST_F(fixture_class_name, test_name)
{
    // test body has full access to fixture members
    // SetUp() has already run before this
    // TearDown() will run automatically after this
}
Defines a test that uses a fixture class, giving it access to the fixture's member variables and helper functions.

Google Test automa­tically calls SetUp() before the test and TearDown() after, with a fresh fixture instance each time.

The second argument is the test name, which must be unique within the fixture.

EXPECT­_FALSE

EXPECT_FALSE(condition);              // marks failed but continues running
ASSERT_FALSE(condition);              // stops test immediately if true
A Google Test macro that checks if a condition is false and marks the test as failed if it isn't.

Unlike ASSERT­_FALSE, the test continues running after a failure so other checks can still be evaluated.

Use when you want to verify something does not exist or did not happen.

EXPECT_NE

EXPECT_NE(actual_value, other_value);   // fails if they are equal

// common usage - string::find returns npos if not found
EXPECT_NE(my_string.find("target"), std::string::npos);  // passes if "target" was found
EXPECT_NE(result, 0);                   // passes if result is not zero
A Google Test macro that passes when two values are not equal, and marks the test as failed if they are equal.

Unlike ASSERT_NE, the test continues running after a failure so other checks can still be evaluated.

Commonly used to verify a search or find operation returned a valid result.