Paul's Python CoursePPC

Project 14 of 20, for Caitlin

Testing the Bank

Tests for Caitlin's festival fund accounts that pass on the real thing and fail on broken copies, then two new features written tests first.

About an hour and a half, in 14 steps.

Sign up free to open itLog in

Act 2 needs a free account, so your work is saved as you go. Act 1 is open to everyone.

The brief

  • CaitlinThe festival fund has real money in it now, but Aaesha won't pay in until it's been tested.
  • AaeshaMy requirements are, one, the accounts are right, and two, they stay right.
  • AaeshaAnd three, if someone breaks them we find out by lunchtime, instead of the week of the festival.
  • PaulThat's three requirements, and one of them is tests.
  • AaeshaActually, all three are tests.

The festival fund's Account is in bank.py, the class you wrote for Caitlin. Write tests for it in test_bank.py: plain functions whose names start with test_, each checking one thing with assert. Your tests must pass on the real bank.py and fail on broken ones.

Then Aaesha's two new requirements, tests first:

  1. transfer(other, pence) moves money from one account into another and returns True. If there isn't enough, nothing moves and it returns False. An amount that isn't more than 0 raises a ValueError.
  2. add_interest(percent) pays in that much interest on the balance, rounded to the nearest penny, and returns it. Interest that rounds to nothing isn't paid in at all.
What it prints
passed test_deposit
passed test_withdraw
passed test_withdraw_everything
passed test_too_much
passed test_str
passed test_deposit_nothing
passed test_transfer
passed test_transfer_too_much
passed test_transfer_everything
passed test_transfer_nothing
passed test_interest
passed test_interest_rounds_to_the_penny
passed test_no_interest_on_nothing

What you learn

What's in it

  1. Checking what you believe
  2. Checking what you believe
  3. Where the bugs live
  4. Exactly everything
  5. A test is a function
  6. Tests that can't fail
  7. How pytest does it
  8. Tests before code
  9. How Paul would do it
  10. Fix Caitlin's version
  11. Tests first
  12. Make the bank pass
  13. Ship it