Assertions and Matchers
Write expressive checks.
Assertions and Matchers is a free C++ Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
Assertions Express Expectations
GTest assertions check conditions inside a test. They come in two severities.
EXPECT_*: failure is recorded, test continuesASSERT_*: 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_NEEXPECT_LT,EXPECT_LEEXPECT_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,LtContains,ElementsAre,UnorderedElementsAreHasSubstr,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
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Assertions and Matchers” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Assertions and Matchers”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
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
- Setting Up GTest
- Assertions and Matchers
- Fixtures and Setup
- Mocking with GMock