A Simple Example of unittest Module

Let's test a simple example of unittest module.

We always find a code example or two to be the quickest way to learn how something new works. So let’s create a little module that we will call mymath.py. Then put the following code into it:

Press + to interact
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(numerator, denominator):
return float(numerator) / denominator
print(divide(34,5))
print(add(4,5))
print(subtract(3,4))
print(multiply(3,4))

This module defines four mathematical functions: add, subtract, multiply and divide. They do not do any error checking and they actually don’t do exactly what we might expect. For example, if we were to call the ...

Access this course and 1400+ top-rated courses and projects.