Content · Programming · Python

Python Understand Arrange, Act, Assert

Organize small tests as Arrange, Act, Assert.

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

Organize small tests as Arrange, Act, Assert.

What It Does and Why It Matters


Arrange creates the required data and dependencies. Act performs the one operation under test. Assert checks the observed result against the expectation. Keeping the three stages distinct helps identify which behavior failed and keeps setup unrelated to the assertion out of the way.

An expected result should come from the requirement, not from blindly copying the implementation.

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 normalize(text: str) -> str:
    return " ".join(text.split()).lower()
# Arrange
raw = "  HELLO   World "
expected = "hello world"
# Act
actual = normalize(raw)
# Assert
assert actual == expected
print(actual)

How the Code Works


  • The definition of normalize creates reusable behavior; its body runs only when called or instantiated as appropriate.
  • The name raw receives the value of ' HELLO World '. Changing this input lets you explore a different case.
  • The print calls display the results. Compare these results with the method’s documented return type and the input used.
  • Arrange sets raw and expected, Act calls normalize(raw), and Assert compares actual to expected. A mismatch raises AssertionError before the success print.

Expected output:

hello world

For the demonstrated inputs, Python displays 'hello world'. 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


Write Arrange/Act/Assert stages for a function that clamps negative numbers to zero.

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


  • Asserting a value calculated by duplicating the same buggy algorithm used in the function under test.
  • Checking only the easiest input and missing a relevant error or boundary case.

Key Takeaways


  • Organize small tests as Arrange, Act, Assert.
  • Arrange creates the required data and dependencies.
  • Write Arrange/Act/Assert stages for a function that clamps negative numbers to zero.
Share this resourceSend this page to someone who may find it useful.