LightMySky

Unit Tests: Deciding What Is Worth Testing

Writing small automated checks that call a function with chosen inputs and compare its behaviour with a specified contract. Choose ordinary cases, relevant boundaries and specified errors; empty input matters when the contract makes it relevant.

No account needed. Progress saves in this browser.

What a learner can do afterwards

  • Write tests for one function covering a normal case, a boundary and an error case
  • Change the function so a test fails, and read what the failure tells you
  • Explain how copying implementation logic into an expected result can hide a shared mistake

1 · Read

A unit test automatically checks a specified behaviour. Assume split_bill(total, people) accepts a nonnegative total and a positive integer people, returns total / people, and raises ValueError otherwise. Plan ordinary cases, relevant boundaries and specified errors for this contract, not a fixed test quota. ```python assert split_bill(16, 4) == 4 ``` The assertion fails if this call's result is not 4.

A boundary depends on the allowed domain. For a function accepting integers from 1 through 6, the endpoints 1 and 6 are boundaries; 3 is an interior value. Empty input and zero matter only when the function's contract makes them relevant. State the expected behaviour before choosing a test.

split_bill(28, 4)expected: 7observed: 6check fails: 6 != 7
A useful failure identifies the call, its expected result and what actually happened.

Check a test by changing the specific behaviour it should detect. If division is replaced by multiplication, a test of split_bill(12, 3) expecting 4 should reject the new result 36. An unrelated change may leave this test passing. A surviving relevant change suggests inspecting the call, expected value and assertion.

An expected result should come from the contract or an independent calculation. If the test repeats the implementation's calculation, both may share the same mistake and agree on an incorrect answer. Such a test is not automatically worthless, but agreement alone does not establish correctness.

A passing suite establishes only its checks. Inspect inputs and assertions: positive-only tests do not cover negative inputs. A tested case can still be missed by weak assertions or different execution conditions. Plan early; add missing tests later.

Try it together

An expected-error test passes when a matching exception is raised: ```python import unittest check = unittest.TestCase() with check.assertRaises(ValueError): split_bill(12, 0) ``` If the call returns normally, this check fails. An unexpected exception does not make it pass. The function raising the expected error is different from the test failing.

Choose cases from the contract. Compare observed behaviour with independent expectations. Check specified exceptions and inspect what the suite actually covers.

Unit-test teaching, examples and assessment repairs independently written for this dataset.

The three testing readings were independently written for this dataset. The retained Unit testing video is referenced from Khan Academy.

2 · Watch

Take it off screen

Print a worksheetA4 with an answer key page for grown-ups. No screen, no internet.

Where it sits

Then practise

8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.

Spotted a problem on this page? Tell us
Unit Tests: Deciding What Is Worth Testing · Computing, ages 17 to 18 · LightMySky