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 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;
}All lessons in this course
- Setting Up GTest
- Assertions and Matchers
- Fixtures and Setup
- Mocking with GMock