Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

testing

What is Assertion Testing?

Hassaan Waqar

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:

What is 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 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 True

print checkLimit(100) # Normal Case
print checkLimit(-10) # Error Case
print checkLimit(0) # Border Case

RELATED TAGS

testing

CONTRIBUTOR

Hassaan Waqar
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring