Introduction to Testing

Assertive Defensive Programming

Overview

  • Teaching: 10 min
  • Exercises: 5 min

Questions

  • How can we compare observed and expected values?

Objectives

  • Assertions are one line tests embedded in code.
  • Assertions can halt execution if something unexpected happens.
  • Assertions are the building blocks of tests.

From your cloned library on notebooks.azure.com, run the library and launch a new Python3.6 notebook.

Assertions are the simplest type of test. They are used as a tool for bounding acceptable behavior during runtime. The assert keyword in Python has the following behavior:

Assert raises and Assertion Error if the test condition is not True:

That is, assertions halt code execution instantly if the comparison is false. If the test condition is True the assert is passed silently:

These are therefore a very good tool for guarding the function against foolish (e.g. human) input. Let's consider a variation on the mean function that we created earlier:

Let's perform a check to see whether the function calculates the mean as we would expect:

Now let's create an empty list, and pass it to our function. What is going to happen and why?

We receive a ZeroDivisionError: division by zero error and an indication of which line the error occurs on.

Use assert to catch divide by zero

With a short code it is relatively trivial to track down the source of this error, however in more complicated codes this may not be the case. To mitigate having to spend time we can spend tracking down errors we can use assert to help us track this down. Re-write the mean function using an assert instruction to raise an AssertionError instead of a ZeroDivisionError.

Solution

The function now exits with an assertion error and indicates where the assertion happened and provides a message to help identify what caused the problem.

The advantage of assertions is their ease of use. They are rarely more than one line of code. The disadvantage is that assertions halt execution indiscriminately and the helpfulness of the resulting error message can be quite limited.

Also, input checking may require decending a rabbit hole of exceptional cases. What happens when the input provided to the mean function is a string, rather than a list of numbers?

First let's make sure that we have the most recent version of our mean function defined:

Now create a new list containing numbers and string(s) and call the function on it:

Note that now we do not see an assertion exit but a TypeError.

Checking type

It would be useful to perform a check that the list, only contains integers.

Hint: One option is to use the isinstance function.

Using isinstance or otherwise, modify your mean function so that it raises as AssertionError and provides a suitable advisory message.

Solution

Asserting with numbers (Integers)

Try the following:

This is exactly what we expect would happen, as long as we are not living in 1984 inspired dystopia. These assertions are comparing integer values.

Asserting with numbers (Floats)

Let's now try a similar experiment with floats:

Perhaps we are living in a dystopian post-truth world afterall. Actually something more subtle is going on, to do with how a computer represents numbers in memory, remember we mentioned that the precision of floats can lead to unexpected behaviour, this is an example. This makes it dangerous, to make direct comparisons of floating point numbers, which is explored further in the remainder of this episode.

Testing 'near equality': Numpy to the rescue

Thankfully we do not have to worry about how to deal with numerical precision ourselves. The wonderful numpy developers have provided a module to help us.

We we want to compare two floats we must not test 'equality' but 'proximity'. We can do this with numpy's assert_almost_equal function:

Key Points

  • Assertions are one line tests embedded in code.
  • The assert keyword is used to set an assertion.
  • Assertions halt execution if the argument is false.
  • Assertions do nothing if the argument is true.
  • Assertions allow us to provide an error message to help track down the source(s) of errors
  • The numpy.testing module provides tools for numeric testing.
  • Assertions are the building blocks of tests.