6) Testing Lesson

Why Create a Python Test

7 min to complete · By Martin Breuss

In the previous section, you learned to catch exceptions to handle situations when your program would otherwise quit. Exceptions make your programs safer, but there's much more that you can do. One of the main approaches in software development to improve the stability of your programs, find and fix weaknesses, and make it easier to collaborate with others is writing tests. Ready to write a test? Okay, let's go, the timer is running:

A person filling a test sheet with a pencil - Photo by https://unsplash.com/@anniespratt Annie Spratt on Unsplash

Photo by Annie Spratt https://unsplash.com/@anniespratt

Nah, no worries! This section isn't about that type of test :)

You won't be the one who's being tested, but you'll write code to test your code. So, if anyone will be sweating in their seat, it'll be your Python code!

Testing is an important topic in software development, with many different directions and about the same amount of different opinions. This can make it feel overwhelming at first. However, it doesn't have to be overwhelming, and you don't need to be a testing pro in order to get started writing some tests for your code.

Before you start learning about some of the most common testing approaches in Python software development, first take a moment to think about why you would want to test your code.

Benefits of Python Testing

So you wrote a cool script, and you had success and fun running it. Everything worked fine, so... why add a test?

Avoiding Frustration

Testing your code assures that as many possible edge cases that may go wrong have been explored and considered. It helps you to share your programs more confidently with friends and family and ultimately with your employer and your customers without needing to worry that they may run into an unexpected problem. You may remember the frustration of unexpected errors from working with your operating system:

Error Message Without A Fix

You definitely want to avoid this type of frustration!

Allowing Changes

By adding tests to your code, you also do your future self a favor. Having the code of your programs well tested allows you to make changes while remaining confident that the changes you introduced didn't break any of your program's functionality.

Facilitating Collaboration

Another reason is that you might be working on a project together with someone else, but you need to be sure that the changes you make won't break what they are working on, and vice-versa. Some programmers won't even consider collaborating on an open-source project if there are no tests that cover the codebase.

Proving Your Professionalism

Finally, if you have experience with writing tests and can show well-written test suites for your portfolio projects, many companies will be much more likely to consider you as a candidate and potential employee. Companies know the value and importance of well-tested code because if something goes wrong, it directly impacts their business.

How You Can Test Your Code

Now, if you agree that there is value in learning how to write tests for your code, the next question that comes up is: How do you write tests? And with that, the whole avalanche of endless options and differing best practices comes rolling down the testing hill.

The good news is that you don't have to worry about all of the different possibilities as of now. Think of testing as a slow and iterative process, just the same as you do about learning to code. Now burn the following sentences into your brain:

  • You don't need to test your whole code
  • You can keep adding tests bit by bit
  • One test is better than no test

Start off slowly. Experiment with Python's unittest module, one very widespread way of testing that extends beyond the Python language, and remember:

One test is better than no test.

Let's take a look at Python's unittest module next.

Summary: Why Create a Python Test

  • Python testing can improve the stability of your programs, find and fix weaknesses, and make it easier to collaborate with others
  • There are many ways to test your program
  • You don't need to test your whole code
  • One test is better than no test

Advantages of Python Testing

  • Avoiding Frustration
  • Allowing Changes
  • Facilitating Collaboration
  • Proving Your Professionalism