assertion
A check that a condition is true, which stops a test or program with a failure if it is false.
In more detail
Test frameworks each provide assertion helpers, such as equal to, contains or throws, that compare an expected value with the actual one and report both when they differ. Some languages and tools also have a built-in form, such as Python's assert statement or Ansible's assert module. A test whose assertions can never fail proves nothing.
Examples
ansible.builtin.assert:
that: result.stat.existsMolecule verification uses assertions to prove the end state a role promises.await expect(page.getByRole("status")).toHaveText("Ready")JUnit: assertEquals("Hello Ada", Greeting.text("Ada"));AssertJ: assertThat(names).containsExactly("Ada", "Lin");assertEquals("Hello Ada", Greeting.text("Ada"));In this course: In Python and pytest an assertion is the plain assert statement followed by an expression, such as assert total == 1000. When the expression is false, Python raises an AssertionError and pytest reports the values involved. A failed assertion ends the test immediately, so any later assertions in the same test never run.assert charged == 1200Related terms
- Molecule — A testing framework for Ansible roles that creates a disposable test machine, applies the role, checks the result and destroys the machine.
- verifier — The Molecule setting and phase that checks the converged test instance really shows the results the role promises.
- when condition — A task keyword holding an expression; the task runs on a host only if the expression is true there, and is reported as skipped otherwise.
- xUnit.net — A popular open-source unit testing framework for .NET, where [Fact] marks a test method and Assert checks results.
- arrange, act, assert — A three-step shape for a test: set up the starting state, perform the one behaviour under test, then check the observable result.
- web-first assertion — A Playwright assertion that keeps rechecking the page until the condition is true or a timeout expires.
- end-to-end test — A test that drives the whole system from the outside, often through a browser or public API, with every part running.
- AssertJ — A Java library of fluent assertions, starting with assertThat, that read like sentences and explain failures in detail.
- JUnit — The standard testing framework for Java, which discovers methods marked @Test, runs them and reports each failure with the test's name.
- node:assert/strict — The strict version of Node.js's built-in assertion module, where comparisons such as equal and deepEqual never convert types.
- unit test — An automated check of one small piece of code, such as a single function, that runs quickly and on its own.
- assertion rewriting — pytest's trick of rewriting plain assert statements as it imports test files so failures print the values involved.
- assertion message — Your own text added after an assert, following a comma, that pytest prints when the assertion fails.
- one reason to fail — The guideline that a single test should only be able to fail for one cause, even if it contains several assertions.
- exception — An error object a program raises to stop normal flow and report that something went wrong.
Taught in
- Testing With Molecule — Ansible Configuration Management at Scale
- Unit Testing with xUnit and NSubstitute — Modern .NET Core, C# & Enterprise Microservices
- Integration Testing with WebApplicationFactory — Modern .NET Core, C# & Enterprise Microservices
- End-to-End Testing with Playwright: Multi-Tier Suites — Production Full-Stack Engineering
- Full-Stack System Verification: Capstone Integration — Production Full-Stack Engineering
- Testing Basics With JUnit 5 — Modern Java: Virtual Threads & High-Throughput Services
- Fluent Assertions With AssertJ — Modern Java: Virtual Threads & High-Throughput Services
- Testing APIs With MockMvc — Modern Java: Virtual Threads & High-Throughput Services
- Native Test Runner: node:test & node:assert/strict — Node.js Internals & Backend Services
- The Cost And Value Of A Test — Automated Testing, TDD & Quality Engineering
- Arrange, Act, Assert — Automated Testing, TDD & Quality Engineering
- One Assertion Per Reason — Automated Testing, TDD & Quality Engineering
- Introducing pytest — Automated Testing, TDD & Quality Engineering
- Making Failures Explain Themselves — Automated Testing, TDD & Quality Engineering