Why test at all?

A commonly overlooked aspect of data science is properly testing your code. This generally means making sure it works as intended and is free of major bugs. When working on smaller, isolated coding projects and analyses, writing tests may not be as important and at times, may be skipped entirely. As a project grows in size and complexity, more users start to interact with it. Everything will still work fine… until it doesn’t.

This is where testing comes in. Writing tests is essential to be able to maintain clean and usable code. It may not be the most exciting or glamorous side of data science, but it certainly is necessary for preventing problems with your code later on. A sufficiently large and complex code base will inevitably contain bugs and issues. Keeping these bugs and issues to a minimum will largely depend on the comprehensiveness of your testing. It will also help when adding new functionality or refactoring your code, making sure you haven’t broken anything you didn’t intend to.

So where do we start? For those of us who do not come from a traditional programming background, writing testing may seem like a daunting task. In this post, I’ll break down the basics of testing Python code and hopefully eliminate those bug-induced headaches later on.

Making assertions

The most basic way to add tests in your code is through assert statements. This built-in keyword allows you to pass in a given condition and check if it is Trueor False.

assert 1 == 1
>> True

We can add an error message after the initial condition to be printed when the assertion is False. The moment an assert condition is evaluated as False, an error is raised and the error message is shown.

assert 1 == 2, "The assertion is False"
>> AssertionError: The assertion is False

#programming #data-science #unittest #python

The Importance of Testing Your Python Code
2.00 GEEK