Concept Explanation
Testing focus means checking behavior on purpose instead of assuming that a program is correct because it compiled once. In this lesson, you will use a small C++ example to see how a direct assertion can protect a basic function from silent mistakes. The point is not to build a large test suite yet. It is to practice thinking in expected results: given this input, what should happen, and how can I verify it immediately?
Where to Put the Code
- Start with variables and inputs. Keep includes, main function, and data types explicit.
- Add processing logic in the middle section.
- Finish with output and quick validation.
Command Reference
- State the expected output for each test case before you run the program.
- Add one more assertion that checks a different but valid input pattern.
- Keep the tested function simple so the purpose of each check is easy to understand.
- Use the program output to confirm when all assertions have passed.
Step-by-step Guide
- Run the example once and confirm that both checks complete without failing.
- Read each assertion and explain what behavior it is protecting.
- Add one more test case for a small edge condition such as adding zero.
- Break one assertion on purpose, run the code, and observe how the failure is reported.
- Restore the correct expectation and verify the program passes again.
Practice Exercises
- Write a similar test for a subtract function with two different inputs.
- Add a test case that checks repeated values such as add(4, 4).
- Create a tiny report message that prints only after all checks succeed.
Coding Challenges
- Design three small tests that would quickly catch a bug in a basic arithmetic helper.
- Compare testing with assert against testing only by printing values, and explain which gives faster feedback here.
Mini Practice Tasks
- Rename one parameter so the function reads more clearly.
- Add one successful assertion and rerun the program.
- Write one sentence describing what the tests guarantee.
Common Mistake
Skipping input validation or mixing logic/output in one unstructured block.
Real-life Mini Challenge
Build a small real-life example for this lesson topic using 3 clear steps: input, process, output.