Introduction to Testing

Basics of Testing

Overview

  • Teaching: 5 min
  • Exercises: 0 min

Questions

  • What are tests?

Objectives

  • Understand what a test does.
  • Understand the place of testing in a scientific workflow.
  • Understand that testing has many forms.

The first step toward getting the right answers from our programs is to assume that mistakes will happen and to guard against them. This is called defensive programming and the most common way to do it is to add alarms and tests into our code so that it checks itself.

Testing should be a seamless part of scientific software development process. This is analogous to experiment design in the experimental science world:

There are many ways to test software, such as:

Key Points

  • Tests check whether the observed result, from running the code, is what was expected ahead of time.
  • Tests can be written before the code they are testing is written (Test Driven Development), however some tests must be written after the code is written (e.g. Regression testing).
  • Assertions and exceptions are like alarm systems embedded in the software, guarding against exceptional bahavior.
  • Unit tests try to test the smallest pieces of code possible, usually functions and methods.
  • Integration tests make sure that code units work together properly.
  • Regression tests ensure that everything works the same today as it did yesterday.