Testing
Testing as you build
Testing checks whether a program behaves as intended and helps find faults. Iterative testing happens throughout development: test a small part, fix problems, then add the next part. Final testing checks the completed program against its requirements and test plan.
A syntax error breaks the language's rules, such as forgetting Python's colon after if age >= 11. A logic error lets the program run but gives a wrong result, such as using age > 11 when age 11 should also be accepted. Testing with expected results helps reveal both kinds of error.
Key terms
Test data types
Suppose a program accepts an age from 11 to 16 inclusive. Choose test data to check typical and difficult cases:
- Normal:
14is an ordinary allowed value; expect it to be accepted. - Boundary:
11and16are the lowest and highest allowed values; expect both to be accepted. - Invalid:
10and17are numbers outside the allowed range; expect them to be rejected. - Erroneous:
fouris the wrong type for an age; expect the program to report the problem and ask again, not crash.
Using several kinds of data is more useful than trying only a typical value.
Key terms
Test plan: age checker
A test plan records the input for each test and the result that should happen. This complete plan checks the age rule from 11 to 16 inclusive:
| Test | Data | Type | Expected result |
|---|---|---|---|
| 1 | 14 | Normal | Accept; report valid age |
| 2 | 11 | Boundary: minimum | Accept; report valid age |
| 3 | 16 | Boundary: maximum | Accept; report valid age |
| 4 | 10 | Invalid: below range | Reject; ask for an age from 11 to 16 |
| 5 | 17 | Invalid: above range | Reject; ask for an age from 11 to 16 |
| 6 | four | Erroneous: not numeric | Explain that a number is needed; ask again without crashing |
When running the tests, record the actual result too and compare it with the expected result. If they differ, correct the program and repeat the tests; a change can introduce a new fault elsewhere.