Unit testing

A software testing method by which individual units of source code … are tested to determine whether they are fit for use.

A little bit of Python

Let’s write a little bit of Python to set the scene:

def factorial(n):
    if n < 2:
        return 1
    return n * factorial(n - 1)
def is_perfect_number(n):

    if n < 0:
        raise ValueError("The number %s is negative!" % n)

    if n in [6, 28, 496, 8128, 33550336, 8589869056, 137438691328, 2305843008139952128]:
        return True
    return False

These may be simple functions, but the code we write is not always completely straightforward.

Often uncaught sources of error can include:

We write code to support the science we are doing, whether that is:

We have a duty to ensure results obtained from the scripts we write are:

Software often splits naturally into units

As a general rule, functions should express a single simple concept. Functions may be chained together to express more complicated control flows.

If every function is simple, and we can verify it is correct, and takes proper account of edge cases, then our scripts that chain multiple functions together stand a much better chance of being fit for use.

Unit testing is the practice of ensuring each function is correct.

» Demo of how to unit-test the python example


« back