liveBook · Manning

Assertion

As explained in Python How-To

A software assertion is an executable check of an expected condition or property that signals failure when the check does not hold.

An assertion is an executable check of a condition. In Python, assert continues when the condition is true and raises AssertionError when it is false; it checks validity rather than producing a Boolean value. section 2.1.2 section 2.1.3

Assertions can check that a formatted string matches an expected value or that a function returns the expected result. For custom class instances, their dictionary representations can be compared as a proxy for equality. section 2.1.2 section 13.3.1

assert create_task("Laundry,3").__dict__ == Task("Laundry", 3).__dict__

This check compares the created task’s attributes with those of an expected task. section 13.3.1

How Python How-To covers it

How deep it goes
Defines truth-dependent execution and demonstrates equality checks for formatted strings and function results.
Examples
Uses formatted-string comparisons and a task-creation function checked against an expected task.
What you can do after
Evaluate expected values by asserting formatted strings or comparing a function’s result with an expected object representation.
How it connects
Connects assertions with f-strings, expressions and statements, and function testing.

Also explained in

Compare how 15 books explain assertion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.