liveBook · Manning

Assertion

Also called assert

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

How 18 Manning books explain it

Build Python Web Apps with Streamlit

An assertion is a test check that evaluates an expected condition and reports a failure when the condition is false.

How it is used
Checks expected backend behavior and frontend state in automated tests.
How deep it goes
Defines assertions and works through conditions, mock-call checks, failure reports, and custom messages.
Examples
Uses addition results, movie search API responses, sidebar elements, and displayed movie results as checks.
What you can do after
Apply it via pytest to compare expected and actual behavior and inspect failures.

Read how Build Python Web Apps with Streamlit explains assertion →

Modern C

assert is a C standard library macro that evaluates a runtime condition and reports a diagnostic followed by abnormal termination when the condition is false.

How it is used
Uses assertions to make function preconditions explicit and expose false runtime assumptions during debugging.
How deep it goes
Explains evaluation, failure through abort, and disabling with NDEBUG, supported by a recursive function and wrapper.
Examples
Uses a recursive greatest-common-divisor function and its wrapper to check argument ordering and nonzero inputs.
What you can do after
Diagnose false runtime assumptions with assert and disable its checks for production using NDEBUG.

Read how Modern C explains assertion →

Unit Testing Principles, Practices, and Patterns

An assertion is a test statement that checks whether an observed result matches an expected condition.

How it is used
Uses assertions to verify observable outcomes instead of treating executed code or coverage as evidence of correctness.
How deep it goes
Explains explicit and implicit outcomes, then stops short of prescribing a complete assertion design method.
Examples
Uses IsStringLong, Parse, a recorded property, and an assertion-free test to contrast verification with execution.
What you can do after
Diagnose tests that execute code without verifying all relevant outcomes.

Read how Unit Testing Principles, Practices, and Patterns explains assertion →

The Art of Unit Testing 3e

An assertion is a test condition that verifies whether an actual result or behavior satisfies an expected truth condition.

How it is used
Verifies expected results or behavior in tests, distinguishing a meaningful check from code that merely runs.
How deep it goes
Builds a manual equality check and reusable helpers, then examines how failed assertions stop tests and how overly broad checks become fragile.
Examples
Uses a sum result, a callback and return value checked separately, and verifier outputs whose order or wording need not be fixed.
What you can do after
Practice/perform focused assertions that reveal failures without tying tests to irrelevant output details.

Read how The Art of Unit Testing 3e explains assertion →

Software Engineering for Data Scientists

An assertion is a check that evaluates a condition and reports failure when the condition is false.

How it is used
Guards data-processing workflows and unit tests by checking conditions that must hold before work continues.
How deep it goes
Explains assertion failure, custom messages, and the choice between assert and raise without detailing Python internals.
Examples
Uses unequal numbers, an empty database-query result, and function outputs checked against expected values.
What you can do after
Apply it via Python assert statements to halt invalid data workflows and check function behavior.

Read how Software Engineering for Data Scientists explains assertion →

Data-Oriented Programming in Java

An assertion is an executable check that a specified condition holds for a value, state, or computation.

How it is used
Uses executable checks to validate invariants, broaden test assumptions, and guard code with proof-bearing data types.
How deep it goes
Works through exhaustive state generation, multi-state combinations, and randomized test data, but leaves randomization problems for later treatment.
Examples
Demonstrates integer arithmetic, RawData rows, pairwise operations, ValidCart boundaries, and randomized Account fixtures.
What you can do after
Diagnose/debug with it by checking invariants across explicit, exhaustive, relational, or randomized test states.

Read how Data-Oriented Programming in Java explains assertion →

Modern C, Third Edition

assert is a C standard-library assertion macro that evaluates a runtime condition and calls abort when the condition is false.

How it is used
Uses runtime assertions to expose violated assumptions and function preconditions during execution.
How deep it goes
Explains evaluation, diagnostics, abort, and NDEBUG disabling, but stops short of implementation details for the macro itself.
Examples
Uses gcd2 precondition checks and the diagnostic produced when an assertion fails.
What you can do after
Diagnose debugging-time violations of runtime assumptions and make function preconditions explicit.

Read how Modern C, Third Edition explains assertion →

Build Financial Software with Generative AI (From Scratch)

An assert statement checks that a condition is true during program execution and signals an assertion failure when it is false.

How it is used
Uses assertions to validate parser results and API responses in unit, behavior-driven, and load tests.
How deep it goes
Shows assertions with failure messages and response-field checks, including correction of an incorrect expected status code.
Examples
Checks a parsed payment-file header, returned filenames and credit amounts, and HTTP status codes for file endpoints.
What you can do after
Apply assertions using pytest and Locust to check expected results and investigate test failures.

Read how Build Financial Software with Generative AI (From Scratch) explains assertion →

100 Java Mistakes and How to Avoid Them

An assert statement is a Java language statement that evaluates a boolean condition and raises an assertion failure when the condition is false and assertions are enabled.

How it is used
Uses assertions to check invariants early during testing or staging and locate unexpected program states near their causes.
How deep it goes
Explains syntax, JVM enablement, side effects, preconditions, and analysis limits, but stops short of broader assertion design.
Examples
Uses set mutation, matcher processing, prefix normalization, and collection removal to expose assertion misuse and safer alternatives.
What you can do after
Apply assertions safely by separating side effects, enabling them in tests, and replacing failed preconditions with explicit validation.

Read how 100 Java Mistakes and How to Avoid Them explains assertion →

Web Components in Action

How it is used
Uses assertions as the checking component of a testing setup, alongside test organization and browser automation.
How deep it goes
Defines assertions and illustrates expected-value checks, stopping short of explaining assertion-library internals.
Examples
Illustrates arithmetic, a number-doubling function, and slider component values and display state.
What you can do after
Apply Chai assertions to check function results and component state.

Read how Web Components in Action explains assertion →

Secrets of the JavaScript Ninja, Third Edition

An assertion is a regular-expression condition that checks surrounding text without consuming it as part of the match.

How it is used
Constrains regular-expression matches using surrounding text without including that text in the match.
How deep it goes
Explains positive and negative lookbehind, immediate anchoring, and worked patterns; mentions lookahead but does not develop it.
Examples
Uses patterns for kebab-case hyphens, masking names, and distinguishing negated from direct commands.
What you can do after
Evaluate and write regular-expression patterns that condition matches on immediately preceding text.

Read how Secrets of the JavaScript Ninja, Third Edition explains assertion →

Effective Platform Engineering

An assertion is a declarative statement that evaluates as true or false for given data.

How it is used
Applies declarative assertions to policy-as-code decisions about whether supplied data is approved.
How deep it goes
Defines predicates, extensions, data domains, and subject variables, then connects them to true-or-false evaluation.
Examples
Uses book-writing predicates, employee lists, age-based Rego rules, and allow-or-deny policy results.
What you can do after
Evaluate policy inputs by identifying predicates, assigning subject variables, and checking whether assertions hold.

Read how Effective Platform Engineering explains assertion →

ASP.NET Core in Action, 3e

An assertion is a test operation that verifies an actual result satisfies an expected condition.

How it is used
Uses assertions to verify unit-test outcomes and expected exception behavior.
How deep it goes
Explains value and exception assertions with runnable examples, but defers broader ASP.NET Core testing.
Examples
Demonstrates currency conversion, mismatched expected values, multiple InlineData cases, and zero-rate exceptions.
What you can do after
Apply it via xUnit to verify returned values, repeated input cases, and exceptions raised by a method.

Read how ASP.NET Core in Action, 3e explains assertion →

Refactoring to Rust

An assertion is a program check that verifies a condition and reports failure when the condition is false.

How it is used
Uses assertions to verify expected values and conditions, especially within automated Rust tests.
How deep it goes
Explains Boolean and equality checks, panic behavior, failure output, and debugging values without covering assertion implementation internals.
Examples
Demonstrates 2 + 2 assertions, a deliberately failing equality check, and Cargo test failure output.
What you can do after
Apply assertions in Rust tests to validate conditions and inspect reported values when checks fail.

Read how Refactoring to Rust explains assertion →

Practices of the Python Pro

How it is used
Uses assertions as checkpoints for comparing expected and actual values in functional tests.
How deep it goes
Explains Boolean conditions, silent success, AssertionError failure, and testing-tool usage without descending into implementation internals.
Examples
Demonstrates calculate_mean checks, Product SKU tests, ShoppingCart outcomes, dictionary comparisons, and pytest test-run output.
What you can do after
Apply raw assertions in Python tests and migrate unittest comparisons to concise pytest expressions.

Read how Practices of the Python Pro explains assertion →

Learn Rust in a Month of Lunches

How it is used
Uses assertions to catch logic bugs and check expected values, especially in testing.
How deep it goes
Defines three assertion macros and demonstrates passing checks and custom failure messages.
Examples
Checks a name with assert!, assert_eq!, and assert_ne!, including a custom failure message.
What you can do after
Apply Rust assertion macros to check conditions and value equality during execution.

Read how Learn Rust in a Month of Lunches explains assertion →

Software Design for Python Programmers

An assert statement evaluates a condition and aborts execution when the condition is false.

How it is used
Enforces preconditions when continuing after invalid input could corrupt object state or return an invalid value.
How deep it goes
Defines the check and its aborting behavior, while deferring exception handling as an alternative contract.
Examples
Uses an empty CircularBuffer and an unchecked remove() call to demonstrate assertion failure.
What you can do after
Diagnose/debug with it: detect violated preconditions immediately instead of allowing invalid state to propagate.

Read how Software Design for Python Programmers explains assertion →

Python How-To

How it is used
Checks expected conditions in formatting examples and verifies function outputs in tests.
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.

Read how Python How-To explains assertion →

Also covered in

What to know first

Related concepts

Questions about assertion

What is assertion?

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

Which Manning books explain assertion?

Build Python Web Apps with Streamlit, Modern C, Unit Testing Principles, Practices, and Patterns, The Art of Unit Testing 3e, Software Engineering for Data Scientists, Data-Oriented Programming in Java, Modern C, Third Edition, Build Financial Software with Generative AI (From Scratch) and 10 more.

What should I know before learning assertion?

Manning books that teach assertion usually introduce exception, software testing, function and list first.

What kinds of assertion are there?

Kinds of assertion include lookahead and lookbehind assertion.

What is assertion also called?

Assertion is also called assert.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.