Unit Testing in C++
Unit testing verifies the observable behavior of a small C++ component, typically a function or class, against explicit expectations.
Summary
Unit testing verifies the observable behavior of a small C++ component, typically a function or class, against explicit expectations. Reliable tests are deterministic, independent, and focused on the component's contract rather than implementation details.
Arrange-Act-Assert organizes a test into setup, execution, and verification. Test cases should cover typical inputs, boundary values, and invalid inputs defined by the contract. Fixtures provide shared setup and cleanup, while test doubles replace dependencies such as files or network clients to preserve isolation.
GoogleTest provides TEST to define tests and assertions to verify outcomes.
| Assertion | Purpose | On failure |
|---|---|---|
| EXPECT_EQ | Check equality | Continue the current test |
| ASSERT_EQ | Check equality | Stop the current test function |
| EXPECT_THROW | Check for a specified exception type | Report an unmet exception expectation |
Unlike dedicated framework assertions, C++ assert checks can disappear when NDEBUG is defined. Regression tests detect changes that break previously verified behavior, and tests serve as executable examples of component contracts. Integration tests examine interactions between components rather than a single isolated unit.
Common Misconceptions: High code coverage does not prove correctness; passing tests verify only the cases included; a fatal GoogleTest assertion stops the current test function, not necessarily the entire suite.
🧠 Key Concepts
- Unit isolation
- Arrange Act Assert
- Test assertions
- Test doubles
- Test fixtures
- Boundary testing
- Regression detection
- Code coverage
- Exception testing
- Integration testing
🧠 Quick Check
See what you remember from the summary.
In Arrange-Act-Assert, what should a test do immediately after executing the target operation?
🧠 Flashcards Preview
Tap a card to reveal the definition.
Ready to quiz yourself?
Test what you remember with a full practice quiz on this note. Create a free account and start in seconds.
Full Notes
Read the original note content before deciding whether to save or study from it.
Unit Testing in C++
📘 Overview Unit testing checks the behavior of small, isolated pieces of C++ code against explicit expectations. Automated tests expose regressions and verify normal cases, boundary conditions, and error handling.
🧠 Key Idea A unit test supplies controlled inputs to a small unit of code and checks its observable behavior with assertions. Reliable tests are deterministic, independent, and focused on the unit's contract rather than its implementation.
⚔️ Core Details: - A unit is a small testable component, typically a function or a class. - Arrange-Act-Assert separates test setup, execution of the target operation, and verification of the result. - GoogleTest provides TEST for test cases, EXPECT_EQ for nonfatal equality checks, and ASSERT_EQ for equality checks that stop the current test function on failure. - Test cases should cover typical inputs, boundary values, and invalid inputs specified by the unit's contract. - Test doubles replace dependencies such as file access or network clients to keep unit tests isolated and deterministic. - C++ assert checks can disappear when NDEBUG is defined; dedicated test framework assertions remain available in release test builds.
🎯 Why It Matters: - Regression tests reveal when a code change breaks previously verified behavior. - Tests provide executable examples of a function or class contract. - Isolating a component for testing can expose excessive coupling and encourage clearer interfaces. - A passing suite checks only the cases it contains; high code coverage does not prove correctness.
🧠 Quick Recall: - Unit test - an automated check of a small component's behavior in isolation. - Arrange-Act-Assert - prepare inputs and dependencies, execute the operation, then verify the outcome. - Fixture - shared setup and cleanup that gives related tests a consistent starting state. - EXPECT_THROW - a GoogleTest assertion that checks whether a statement throws a specified exception type. - Integration test - a test of interactions between components rather than one component in isolation.
More ways to study when you copy this note
Copy this note into your library to unlock focused practice sessions and long-term review.
Answer all questions first, then see feedback at the end — the way real exams work.
Focuses each session on what you got wrong, not what you already know.
Full timed exam with all questions, no pausing, and results at the end. Built for board exam prep.
Part of 🗄️ BS Information Systems — 1st Year
Browse this Review Set →More Computer Engineering notes
See all →More in Programming Fundamentals
See all →More from NoteLib
Browse NoteLib's public notes →Copy this note to your library and get the full Study Pack instantly — summary, key concepts, and practice quiz included.