Content · Programming · Python

Python First Tests for a Pure Function

Write your first tests for deterministic functions without I/O.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Write your first tests for deterministic functions without I/O.

What It Does and Why It Matters


Pure functions return an output determined by their arguments and do not change external state. They are ideal first test targets: no database, network or clock mocking is required. Add at least one ordinary input and one boundary condition, and make the assertion messages informative.

Test output and edge behavior rather than internal implementation details.

Step-by-Step Example


Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.

def classify(value: int) -> str:
    if value < 0:
        return "negative"
    if value == 0:
        return "zero"
    return "positive"
assert classify(-3) == "negative"
assert classify(0) == "zero"
assert classify(8) == "positive"
print("3 checks passed")

How the Code Works


  • The definition of classify creates reusable behavior; its body runs only when called or instantiated as appropriate.
  • The print calls display the results. Compare these results with the method’s documented return type and the input used.

Expected output:

3 checks passed

For the demonstrated inputs, Python displays '3 checks passed'. This result follows from the operation described above; change one input at a time to understand which rule matters.

What to Notice


Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.

If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.

Where You Would Use This


Start with an independently checkable requirement and test both normal and boundary inputs. A passing assertion documents only the cases that were exercised. Keep tests deterministic so a failure identifies a real change rather than environmental noise.

Practice Exercise


Move the three assertions into unittest.TestCase methods.

Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.

Reference: official Python documentation for the complete API contract and version notes.

Common Beginner Mistakes


  • Only checking the common positive case and forgetting the equality boundary at zero.
  • Overlooking the documented condition: pure functions return an output determined by their arguments and do not change external state.

Key Takeaways


  • Write your first tests for deterministic functions without I/O.
  • Pure functions return an output determined by their arguments and do not change external state.
  • Move the three assertions into unittest.TestCase methods.
Share this resourceSend this page to someone who may find it useful.