Writing Test Cases
in C++
Learn how to write simple test cases that verify your code produces expected outputs and catch bugs before they become problems.
What Is a Test Case?
A test case is a small piece of code you write to check whether a function produces the correct result. You give it a known input, run the function, and compare the actual output to the expected output.
Why Write Test Cases?
Anatomy of a Test Case
Every test case has three essential parts, often called the Arrange-Act-Assert pattern:
The Core Testing Pattern
Using assert() for Tests
C++ provides a built-in assert() macro from <cassert>. If the condition is false, the program crashes with an error message showing the exact line that failed.
assert() vs if/else Testing
| assert() | if/else Pattern | |
|---|---|---|
| Header | <cassert> | None needed |
| On Failure | Crashes program | Prints message, continues |
| Shows All Results | No, stops at first fail | Yes, runs all tests |
| Code Length | One line | 3-5 lines per test |
| Best For | Quick checks | Detailed reporting |
Choosing Edge Cases
Edge cases are unusual or extreme inputs that often reveal bugs. Good testers think beyond the "happy path" and deliberately try inputs that could break things.
Step-by-Step Test Writing
Example 1: Testing isEven()
Example 2: Testing maxOfThree()
Example 3: Testing String Functions
Organizing Multiple Tests
As your project grows, group tests into dedicated functions and call them all from main(). This keeps your code clean and each test function focused.
Test Coverage Thinking
Good coverage means your tests exercise all the important paths through your function. Think of inputs as falling into categories:
Common Mistakes
Test Case Cheat Sheet
| Concept | What to Do | Example |
|---|---|---|
| Arrange | Set up inputs and expected value | int expected = 5; |
| Act | Call the function | int result = add(2, 3); |
| Assert | Compare actual vs expected | assert(result == 5); |
| Edge Case | Test zero, negatives, empty | assert(add(0,0) == 0); |
| Organize | Group into test functions | void testAdd() { ... } |
Question 1 of 5
What are the three parts of the Arrange-Act-Assert pattern?
Question 2 of 5
What happens when an assert() statement's condition evaluates to false?
Question 3 of 5
You are testing a function abs(int n) that returns the absolute value. Which set of test inputs provides the best coverage?
Question 4 of 5
Consider this function and test:
bool isPositive(int n) { return n > 0; }
assert(isPositive(5) == true);
assert(isPositive(-3) == false);
A student claims these two tests provide complete coverage.
What critical edge case is missing from these tests?
int, so decimals and strings are not valid inputs.Question 5 of 5
A student writes this test function:
void testDivide() {
assert(divide(10, 2) == 5);
assert(divide(9, 3) == 3);
assert(divide(7, 2) == 3);
cout << "All passed!" << endl;
}
The divide function performs integer division.
Which of the following is the most important missing test case?