Testing Scientific Code

What is a software test?

Software tests are written in order to check that the implementation of software is correct, and to guard against dangerous behaviour. They’re used to make software run in a safer manner; if we know what the inputs and outputs of sofware should be, and we know that for those inputs the answer is correct, we can have some confidence that the software is correct.

Why is this important? Consider how science works in experimental science. We try and apply the scientific method:

Scientific Method

A set of rough principles:

  • Ask a question.
  • Form a hypothesis.
  • Make a prediction based on the hypothesis.
  • Design an experiment to test the hypothesis.
  • Build the equipment
  • Calibrate equipment by testing it
  • Run the experiment
  • Detail carefully the procedures and record information.
  • Draw conclusions from results.

Consider whether, when writing software to do science, you check that your ‘equipment’ is calibrated? In many cases, the answer will be no.

Why might this be the case? Often ad-hoc changes are made to code which mean things stop working. Have you ever come back to a script that you wrote and found it no longer works because you changed a file somewhere else? These issues become especially common when multiple people, who might not have oversight over what you are using it for, are working on software. It’s important to test the relevant parts of your code.

In this tutorial, we’ll step through some examples in the Python language which aim to show how you can write tests, and incorporate them into your own software in practice.

Principles of Software Testing

Static vs Dynamic Testing

Static Testing

Dynamic Testing

Why is it difficult for science?

Why is it crucial for science?

Dynamic Testing: Types of Test

Unit Tests

$$\sin{\left(n\pi\right)} = 0 \,\,\,\,\, \forall n \in \mathbb{Z}$$

Integration Tests
Regression Tests

Aside: Test Driven Development

Methodology for testing where tests are written before writing any code.

This means that…