assert

noun

say it: “uh-SURT” /əˈsɝt/

A Python statement that checks that something is true and stops with an AssertionError if it isn't; pytest tests use it to say what the right answer is.

What does assert do?

assert checks that something is true and stops with an error if it isn't. Tests use it to say what the right answer should be.

discount = 0.2
assert 0 <= discount <= 1, "discount must be between 0 and 1"

In a sentence

  • “The test says assert total(3) == 7.50.”
  • “An assert at the top of the function stops it if the discount is above 1.”

Level: Journeyman · More Python terms at this level

Updated