...
/A Simple Example of unittest Module
A Simple Example of unittest Module
Let's test a simple example of unittest module.
We'll cover the following...
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 + bdef subtract(a, b):return a - bdef multiply(a, b):return a * bdef divide(numerator, denominator):return float(numerator) / denominatorprint(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
...