Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

The mymath module

We have a module called mymath that has two methods: add and div. It is a very simple module with very simple functions, but for our purposes that does not matter.

Testing a function should be calling the function with some parameters and then comparing the result to some expected value.

The complexity of the function does not matter.

In this case each function also comes with some documentation, including examples using the interactive shell.


def add(x, y):
    """Adding two numbers

    >>> add(2, 3)
    5

    """
    return x + y

def div(x, y):
    """Dividing two numbers

    >>> div(8, 2)
    4.0
    >>> div(8, 0)
    Traceback (most recent call last):
    ...
    ZeroDivisionError: division by zero
    """
    return x / y