What is Assertion Testing?
An assertion is a boolean expression. It is used to test a logical expression. An assertion is true if the logical expression that is being tested is true and there are no bugs in the program. Assertion testing can be used at any particular stage of the program.
The illustration below describes an assertion test:
Advantages
Assertion testing has the following advantages:
- Detects errors that are otherwise subtle and difficult to catch.
- Detect errors as soon as they occur.
Disadvantages
Assertion testing has the following disadvantages:
- Does not report errors.
- Failed assertions can have side effects, and other bugs may not be caught.
- Can be time-consuming if there are multiple errors.
- It cannot test all conditions. Some conditions can be tested conceptually, but not practically, through assertions.
Example
The code snippet below shows an assertion test in Python:
-
The
checkLimitfunction tests that thevaluepassed is greater than or equal to 0. -
The failed assertion in
Line 6causes an error, so the subsequent code is not executed.
def checkLimit(value):assert (value >= 0),"Value cannot be less that 0"return Trueprint checkLimit(100) # Normal Caseprint checkLimit(-10) # Error Caseprint checkLimit(0) # Border Case
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved