Write Your First Python Unit Test Today

Wouter van Heeswijk, PhD
Geek Culture
Published in
5 min readJun 12, 2021

--

Take your debugging skills to the next level.

Photo by Nina Mercado on Unsplash

For the individual coder, unit tests might be a foreign concept. In development teams, you can’t live without them. This article will give a brief introduction to the concept of unit testing. Before you know, you’ll be able to do it by yourself!

Manual and automated testing

At some point every programmer experiences them — bugs. Your script crashes or simply does not give the desired output: it’s time to debug. If you are fortunate, you either get a clear error message or know by heart where to look. If not, you are in for a frustrating process of checking the code line by line, manually passing various inputs, adding print statements to check outputs, etc.

The procedure could be formalized though. Good programming practice dictates to divide your code into small, comprehensible functions. If you know the expected input and output of each function, you could evaluate your script function by function to isolate the error.

Manually executing such a procedure would be rather tedious and boring. Programmers are somewhat lazy by nature — that’s why they automate stuff — and always look for ways to make life easier. Unsurprisingly, they also found a way to make testing and debugging…

--

--

Wouter van Heeswijk, PhD
Geek Culture

Assistant professor in Financial Engineering and Operations Research. Writing about reinforcement learning, optimization problems, and data science.