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:
Assertion testing has the following advantages:
Assertion testing has the following disadvantages:
The code snippet below shows an assertion test in Python:
The checkLimit
function tests that the value
passed is greater than or equal to 0.
The failed assertion in Line 6
causes 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