0Pricing
C++ Academy · Lesson

Assertions and Matchers

Write expressive checks.

Assertions and Matchers is a free C++ Academy lesson on CoddyKit — lesson 2 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.

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;
}

Comparison Assertions

The core comparison macros:

  • EXPECT_EQ, EXPECT_NE
  • EXPECT_LT, EXPECT_LE
  • EXPECT_GT, EXPECT_GE
#include <gtest/gtest.h>

TEST(Compare, Numbers) {
    EXPECT_LT(2, 3);
    EXPECT_GE(5, 5);
}

Boolean Assertions

EXPECT_TRUE and EXPECT_FALSE check boolean conditions directly.

#include <gtest/gtest.h>

bool is_even(int n) { return n % 2 == 0; }

TEST(Bool, Even) {
    EXPECT_TRUE(is_even(4));
    EXPECT_FALSE(is_even(3));
}

Floating-Point Comparisons

Never use EXPECT_EQ on floats. Use EXPECT_FLOAT_EQ, EXPECT_DOUBLE_EQ, or EXPECT_NEAR with a tolerance.

#include <gtest/gtest.h>

TEST(Floats, Approx) {
    EXPECT_DOUBLE_EQ(0.1 + 0.2, 0.3);
    EXPECT_NEAR(3.14159, 3.14, 0.01);
}

String Assertions

For C strings use EXPECT_STREQ / EXPECT_STRNE. For std::string, EXPECT_EQ works since it compares by value.

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

TEST(Strings, Compare) {
    EXPECT_STREQ("hi", "hi");
    EXPECT_EQ(std::string("ok"), "ok");
}

Exception Assertions

Verify that code throws the expected exception.

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

void check(int n) {
    if (n < 0) throw std::runtime_error("neg");
}

TEST(Throws, Negative) {
    EXPECT_THROW(check(-1), std::runtime_error);
    EXPECT_NO_THROW(check(1));
}

Custom Failure Messages

Stream extra context into an assertion with << to make failures easier to diagnose.

#include <gtest/gtest.h>

TEST(Message, WithContext) {
    int x = 7;
    EXPECT_EQ(x, 7) << "x had unexpected value " << x;
}

GMock Matchers with EXPECT_THAT

EXPECT_THAT combines a value with a matcher for expressive checks, e.g. EXPECT_THAT(v, ElementsAre(1, 2, 3)).

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <vector>
using ::testing::ElementsAre;

TEST(Matchers, Vector) {
    std::vector<int> v{1, 2, 3};
    EXPECT_THAT(v, ElementsAre(1, 2, 3));
}

Useful Matchers

Common matchers include:

  • Eq, Ne, Gt, Lt
  • Contains, ElementsAre, UnorderedElementsAre
  • HasSubstr, StartsWith, EndsWith

Choosing the Right Assertion

Prefer the most specific assertion: it produces clearer failure messages than a generic EXPECT_TRUE. EXPECT_EQ(a, b) prints both values; EXPECT_TRUE(a == b) does not.

Quick Check

Recall the EXPECT vs ASSERT difference.

Recap

You learned GTest assertions.

  • EXPECT_* continues; ASSERT_* aborts the test
  • Comparison, boolean, float, string, exception macros
  • Stream extra context with <<
  • EXPECT_THAT + matchers for expressive checks

Frequently asked questions

Is the “Assertions and Matchers” lesson free?

Yes — the full text of “Assertions and Matchers” 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 “Assertions and Matchers”?

Write expressive checks. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Assertions and Matchers” 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