Testing your code with pytest

If you’re fairly new to coding chances are you’ve run into an issue where you make a minor change in one place, and then end up breaking your script in another place. In order to find out what went wrong you start adding print statements all over the place to debug your code.

While it sound like a good idea, what you’re actually doing is relying on Python to tell you when you’ve made a syntactical error. However, what if your syntax is find, but your code is incorrect?

For example, say you accidentally changed your addition function to a multiplication function by replacing the + with a *:

Python isn’t going to tell you you’ve made an error because your syntax is perfectly legal. The only way you’ll know that there’s a problem is when you see the result posted from your script. If the result goes unnoticed though, the bug will remain until someone does notice.

Once you’ve realised there is a problem, the print method mentioned at the beginning of this post will help you track down the error, however, it can be very time consuming if this function is part of large script. And, if you were to make the same mistake again, you’d need to repeat the same time consuming process again.

It’s safe to say that I found myself in this situation many times when I first started coding. Just when I was about to begin pulling my hair out I thought to myself, “there has to be a better way!”. And sure enough, there is.

While unittest is part of the Python standard library, I much prefer py.test as it is much cleaner to read and write, and is very powerful too.

Installation

As with just about everything with Python, installation is easy as:

 pytest Basics

In a nutshell, you use pytest to ensure that your code produces the correct results. Let’s take a look at this example in the pytest documentation:

Here we see two functions:

  • f() : Returns the integer 3.
  • test_function() :   assert s that f()  will return 4.

Because the f() returns a result that is different to the test assertion, pytest will alert us to this:

Making minor modifications to this code enables us to create a test for our addition function:

Doing so produces the following results:

And there you have it! Not only do we now know that your code is syntactically correct, but we also know that it is producing the results we expected. If someone makes a change to the function and the wrong figure is produced as a result, pytest will alert you to it.

Conclusion

Using this technique to automate unit testing will save you a massive amount of time because it’s not only automatic, but it tells you exactly where the error is. No longer will you need to include temporary print statements in order to find the source of an error, only to remove them once you’ve fixed the error, but then re-add them again if the error re-occurs. pytest is a clean, elegant and permanent solution.

Update: If you enjoy using pytest, you might also be intersted in my Allure2: A GUI for your code tests post too.

Knowledge Base

See the Coding section of my Knowledge Base for more information.
As always, if you have any questions or have a topic that you would like me to discuss, please feel free to post a comment at the bottom of this blog entry, e-mail at will@oznetnerd.com, or drop me a message on Twitter (@OzNetNerd).

Note: This website is my personal blog. The opinions expressed in this blog are my own and not those of my employer.

Leave a Reply

Your email address will not be published. Required fields are marked *