Checking assumptions with assert

18-minute lesson · module-testing-01

You are previewing this course as a guest

Read anything and run any exercise you like. It all runs in your own browser, so nothing here is saved: no XP, no progress, and the AI tutor needs an account. Sign in and your work starts counting.

Sign in

Checking assumptions with assert

18-minute lesson · module-testing-01

  • Write an `assert` statement that checks a condition and its message form.
  • Explain that a passing assert is silent and a failing one raises AssertionError.
  • Use asserts to express what a function must return for given inputs.
  • Cover boundary cases as well as ordinary cases when asserting behavior.

Source attribution

Status: adapted

How to Think Like a Computer Scientist: Interactive Edition source · Unit Testing / Checking Assumptions About Data With Assert

Adaptation notes. Reading rewritten in our voice. The clamp example is ours. The student writes a function; the hidden tests are a battery of asserts covering ordinary inputs and the boundary values, modeling exactly how tests are written.

License notes. Original chapter is GNU FDL 1.3 (Brad Miller, David Ranum, and the upstream Elkner/Downey/Meyers text). No verbatim text reproduced.