Errors, Debugging & Testing · Lesson 80

Write a Small Unit Test

A unit test runs a small piece of behaviour with known input and checks an expected result automatically.

ConceptWorked examplePracticeKnowledge check
Textbook walkthrough

Write a Small Unit Test

A unit test runs a small piece of behaviour with known input and checks an expected result automatically. A useful test fails when the implementation is wrong and covers both normal cases and important boundaries rather than simply executing the code.

Learning goal: explain why Write a Small Unit Test behaves this way, apply it to a small example, and verify the result independently. Begin by being able to justify this first step: Choose one behaviour of a small function and define the expected result for a controlled input.

Deeper walkthrough

Read Write a Small Unit Test as a mechanism, not a recipe

Treat this as a sequence of observable decisions rather than one opaque command. Stage 1: Choose one behaviour of a small function and define the expected result for a controlled input. Stage 2: Arrange the input/state, call the function, then assert the observable result. Stage 3: Add at least one boundary or invalid case that could fail for a different reason. Final checkpoint: Run the test after a deliberate small code change to confirm that it can actually detect a regression.

Mechanism

Follow the transformation

Choose one behaviour of a small function and define the expected result for a controlled input.

Arrange the input/state, call the function, then assert the observable result.

Add at least one boundary or invalid case that could fail for a different reason.

Evidence

Know what would convince you

  • Trace a tiny input by hand and compare the runtime result.
  • Inspect type, value/shape and any mutation/side effect explicitly.
Useful distinctionInput: Objects/values supplied to the operation.
Click a stage to inspect what happens, what changes, and what should be checked before moving on.
Stage 1

Choose one behaviour of a small…

Choose one behaviour of a small function and define the expected result for a controlled input. This is an input-preparation stage for Write a Small Unit Test. Verify the relevant type, shape, units, keys, missingness or assumptions before later steps depend on them.

Input focus: confirm the data/object, units, type, shape and assumptions before the next operation depends on them.
How it works

Trace the mechanism step by step

  1. Choose one behaviour of a small function and define the expected result for a controlled input.
  2. Arrange the input/state, call the function, then assert the observable result.
  3. Add at least one boundary or invalid case that could fail for a different reason.
  4. Keep the test deterministic and independent of unrelated files, clocks or network state when possible.
  5. Run the test after a deliberate small code change to confirm that it can actually detect a regression.
Worked demonstration

Write a Small Unit Test

# Step 1 — Define the reusable `add_tax` function; its indented body describes what happens for each call.
def add_tax(amount, rate):
    # Step 2 — Return the computed value to the caller so the result can be reused or tested.
    return amount * (1 + rate)

# Step 3 — Define the reusable `test_add_tax` function; its indented body describes what happens for each call.
def test_add_tax():
    # Step 4 — Assert an assumption that must hold here; failure exposes an invalid state early.
    assert add_tax(100, 0.10) == 110

# Step 5 — Call the function with these arguments and use its returned value or side effect in the workflow.
test_add_tax()
# Step 6 — Display the current value explicitly so the result/state can be inspected during execution.
print("passed")
Expected / illustrative result
The assertion is silent when correct; the final message appears only because the expected result matched.
Interpret the result.

For Write a Small Unit Test, trace the specific input through the mechanism above and independently verify one returned value, state change or side effect.

Distinctions & related ideas

Place the concept correctly

InputObjects/values supplied to the operation.
StateNames or mutable objects that may change during execution.
OutputReturned value, side effect, file, plot or exception to inspect.
Use deliberately

When it is appropriate

Use Write a Small Unit Test when it answers a defined question in Errors, Debugging & Testing and its inputs/assumptions match the current data or program state.

Boundary conditions

When to stop or reconsider

Reconsider Write a Small Unit Test when the required information is unavailable, the operation would violate a validation/data boundary, or a simpler operation answers the question more transparently.

Common mistakes

Failure modes to recognise

  • Running the operation on the wrong object/type or in the wrong environment.
  • Inferring correctness from “no exception” without checking the produced value/state.
  • Hiding a boundary case instead of making its behaviour explicit.
Verification

How to check the result

  • Trace a tiny input by hand and compare the runtime result.
  • Inspect type, value/shape and any mutation/side effect explicitly.
  • Run an edge or invalid case and confirm the exception/behaviour is deliberate.
Hands-on practice

Demonstrate understanding

Try this:

Construct a tiny example of Write a Small Unit Test. First choose one behaviour of a small function and define the expected result for a controlled input. Then arrange the input/state, call the function, then assert the observable result. Predict the result before execution and explain one boundary or failure case.

Use the smallest values that expose the language rule. Write the expected value and type first, then compare the actual state/output with that prediction.
Knowledge check

Check reasoning, not memorisation

Which approach best demonstrates understanding of Write a Small Unit Test?

Quick reference

Remember the logic

Step 1Choose one behaviour of a small function and define the expected result for a controlled input.
Step 2Arrange the input/state, call the function, then assert the observable result.
Step 3Add at least one boundary or invalid case that could fail for a different reason.
Step 4Keep the test deterministic and independent of unrelated files, clocks or network state when possible.
Lesson summary

What to remember

  • A unit test runs a small piece of behaviour with known input and checks an expected result automatically. A useful test fails when the implementation is wrong and covers both normal cases and important boundaries rather than simply executing the code.
  • Identify the Python objects and types involved.
  • Running the operation on the wrong object/type or in the wrong environment.
  • Trace a tiny input by hand and compare the runtime result.