Liam Brannigan

Blog posts

Published on: 6th June 2022

Introduction to testing for data science

Testing is one of the most important skills for any data scientist to learn. In my experience, adding tests to your code is the most valuable way you can spend your time given the overall time saving the tests go on to generate.

In this series of blog posts we introduce the most important ideas for testing code in python today. This first blog post looks at the benefits of testing, why you should write tests on different scales and why you should use an automated testing framework.

Benefits of testing

Getting over the intermediate dip

Often a programmer’s learning rate slows done once they are able to write complex code. Beginners hit new errors very quickly - and so learn faster. With complex code it can take minutes (or even hours!) to reach new errors - this greatly reduces the programmer’s learning rate. I call this the intermediate dip. When these intermediate programmers start writing tests they begin to find the errors in their code quickly and so the learning process ramps up again.

Testing on different scales

A code base works on many different scales from a single function to a pipeline composed of many functions. Your tests must also work on these different scales: some of your tests will test the output of a single function while others will test the output of many of your functions chained together.

The terminology used to describe testing is not always consistent but often the fine-grained tests for individual functions are called unit tests while the coarse-grained tests for pipelines are called functional tests, end-to-end tests or integration tests.

In general unit tests are easier to write but functional tests are the most useful as it is the output of the pipeline you are primarily interested in.

When to stop writing tests

In a pure test-driven development (TDD) approach the aim is to have 100% test coverage - that is you have a test for each of your functions. However, I find that this goal is daunting for a lot of people. The good news is that in general, it is not necessary to have 100% test coverage.

The aim is for you to derive value from your tests. For most people going from 0% to 10% test coverages generates the most value if you start by tagetting the most failure-prone part of your pipeline.

Learn more

Want to know more about testing and high performance python for data science? Then you can:

or join me at my next workshop on accelerated data science on June 29th 2022.

Further reading