All learning paths Learn See Try Make Write one test that can fail What this lesson is about A test that always passes proves nothing. Write one that goes red when the code is wrong, and prove it.
You wrote a test for the GST line in a shop's billing program, and it passed at once. What now?
A Add two more tests for the same line, so at least one of them will catch a mistake. B Change the number in the test until it fails, which shows the test can go red. C A test that never fails proves nothing. Break the code and watch it go red.
Check my recall Why this matters A test that always passes is decoration. It costs the same to write and it catches nothing. The only proof that a test works is watching it fail on code you broke on purpose.
Write one test that goes red when the code is wrong, and prove that it does.
A test that always passes is decoration. It costs the same to write and it catches nothing. The only proof that a test works is watching it fail on code you broke on purpose.
A test that never fails proves nothing. Break the code and watch it go red. One test checks one behaviour, so its failure names one thing. Name the test after the behaviour, so a stranger reads the failure and knows. Task Write one test for a function that adds 18 percent GST to a bill amount.
Weak approach Calls the function with three different bill amounts and checks each time that a number came back. Every call passes, and a broken function that always returns zero would pass as well.
Stronger approach Checks that 100 returns 118, then sets the rate to zero and confirms the same test goes red.
Why the stronger approach works A number always comes back, so the weak test cannot fail. The strong one was proved to fail.
Try a changed situation A teammate sends you one test that checks login, the bill total and the printed slip together. It fails. What do you tell her?
Make something yourself Write one test for a small function. Show it passing on the correct code, then break the code on purpose and show the same test failing. Paste both results.
Lesson 5 of 10 on Coding for evaluation work next Find the input that breaks it Part 2 ends in a work sample you can send
Read the primary or official source: Python documentation · unittest