0Pricing
C++ Academy · Lesson

Assertions and Matchers

Write expressive checks.

Assertions Express Expectations

GTest assertions check conditions inside a test. They come in two severities.

  • EXPECT_*: failure is recorded, test continues
  • ASSERT_*: failure stops the current test immediately

EXPECT vs ASSERT

Use ASSERT_* when continuing would crash (e.g. after a null check), and EXPECT_* otherwise so you see all failures at once.

#include <gtest/gtest.h>

TEST(Demo, MixedChecks) {
    int* p = new int(5);
    ASSERT_NE(p, nullptr);
    EXPECT_EQ(*p, 5);
    delete p;
}

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