What Is an Assertion in Testing?
An assertion is a statement in a test that checks whether an actual result matches an expected one, causing the test to fail if it doesn't.
Published November 15, 2026
An assertion is a statement in a test that checks whether a result matches what you expect; if not, the test fails. Without assertions a test proves nothing.
The concept
Typical form: expect(actual).toBe(expected). The framework throws an error with a helpful diff when they differ.
Good tests make specific assertions about behaviour, not just 'it did not crash'.
Pass or fail
expect(add(2, 2)).toBe(4)
✓ passes
expect(add(2, 2)).toBe(5)
Expected: 5 Received: 4
Practical examples
Jest matchers
expect(user.name).toBe('Ada')
expect(list).toContain(3)
expect(obj).toEqual({ a: 1 })
expect(fn).toThrow('bad input')Python
assert add(2, 2) == 4
with pytest.raises(ValueError): parse('x')PHPUnit
$this->assertSame(4, add(2, 2));
$this->assertTrue($user->isActive());Common mistakes
- Tests with no assertions.
- Comparing floats with exact equality (use toBeCloseTo).
- Too many assertions in one test.
Key takeaways
- Assertions define pass/fail.
- Be specific.
- One behaviour per test.
FAQ
toBe vs toEqual?
toBe checks identity (===); toEqual compares structure.
Assertion vs exception?
Assertions are test checks; exceptions are runtime errors in code.