About Testing Your Code in Python (Part 1) - Introduction

2 min read 311 words

Discover why testing your Python code is essential and why pytest is the ideal framework for the job.

Table of Contents

Today’s post starts a series about unit testing with pyTest. We’ll cover why you should test your code and what makes pyTest an excellent choice for testing it.

About the Importance of Testing

Let’s first address the elephant in the room: Why should you test your code at all?

Testing your code gives you peace of mind: Let’s assume you’re the world’s best and smartest Python programmer. Still, as a human being, you’re prone to making mistakes, syntactical or logical. The latter ones are harder to catch, and therefore testing if your code logic behaves as it should, saves you countless headaches.

Tests are a quick way to document your code: No matter what, documenting your code is a crucial part for making your code useful to others, or to remember what you did these many months ago. Unit tests, or their test cases for that matter, can serve as an interim documentation until you have something better, like a proper documentation.

Unit tests got your back and help not only to increase your code’s quality but help you to structure and understand your code much better.

Why using a Testing Framework

Now, that we know the benefits of testing our code, wouldn’t be print statements sufficient? They should help us test our code, and we have some kind of documentation as well. Why should we learn an additional framework?

The point here is about reproducibility. Code is very dynamic, meaning that you will add, change and remove functionalities down the line. A framework provides you the ability to automate the process of checking if your code behaves as you expect.

In a nutshell

Testing your code is important, and you should use a framework, namely pyTest, for testing your code. In the upcoming part we will focus on the process of designing, writing, and executing tests for a sample project.