What is Test Driven Development (TDD)?
Writing tests before we write a function helps us to design and write our functions better.
It makes sure that we think about what we want the software to do before we start writing functions.
Expected Path, Error Path, and Alternate Paths
Whenever we think about the programs we are writing, there are a few things to consider:
- Expected paths: are what we expect the software to do
- Error paths: are what the software should not do
- Alternate paths: are other things that might happen
Cellphone Bill:
If you are calculating a phone bill, with the following costs:
- Call: R2.75
- SMS: R0.65
You receive the input as a list of connections made, which you convert into costs.
- What would the total of the following input be:
- call,sms,call
- What if they’re all sms? All calls?
- What should happen if you receive a different input, eg. 30kb (a data charge)?
- You could ignore the unrecognised data
- You could create a value for data, eg R0.10 /kb
Questions to include in your thinking:
- What is happening in the third Error column?
- What are the two errors for the data input doing? (3rd row)
- What else might happen?
| Input | Expected | Error | Error | Error | Alternate |
|---|---|---|---|---|---|
| call,sms,call,sms,sms | = 7.45 |
< 7.45 |
> 7.45 |
!= 9.55 |
|
| sms,sms,sms,sms | = 2.6 |
< 2.6 |
> 2.6 |
!= 11.00 |
|
| call,30kb,call,sms | = 6.15 |
!= 8.9 |
!= 6.8 |
= 9.15 |