Week 5
Unit tests
A test calls one function and checks the result. You can run that check again whenever you change the function.
A unit test calls one function with an input you chose and checks the return value. Assert says 'this must be true'. If it is not, you get an AssertionError and pytest marks that test failed. The point is to notice a broken function without clicking through the program yourself.
The function under test should return a value, not print it. Print is for the person at the keyboard. Return is for the test. If your old program only printed, this week asks you to pull the decision out into a function the test can call.
A fresh example: a function that doubles a number. One test passes 2 and expects 4. Another passes 0 and expects 0. You run pytest and both should pass. That function is not a problem set.
Problems
- Testing my twttrThe vowel program, split so a function returns the text and the tests call that function.
- Back to the BankThe greeting program, split so a function returns the amount.
- Re-requesting a Vanity PlateThe plate rules, split so a function returns valid or invalid.
- RefuelingThe gauge, split into two functions: one turns a fraction into a number, one turns that number into the gauge.