Automating Unit Tests in Python with Hypothesis

Automating Unit Tests in Python with Hypothesis

Unit testing is key to developing quality code. There’s a host of libraries and services available that you can use to perfect testing of your Python code. However, “traditional” unit testing is time intensive and is unlikely to cover the full spectrum of cases that your code is supposed to be able to handle.

Unit testing is key to developing quality code. There’s a host of libraries and services available that you can use to perfect testing of your Python code. However, “traditional” unit testing is time intensive and is unlikely to cover the full spectrum of cases that your code is supposed to be able to handle. In this post, I’ll show you how to use property-based testing with Hypothesis to automate testing of your Python code. I also discuss some of the advantages of using a property-based testing framework.

Property-based automated testing

Unit testing involves testing individual components of your code. A typical unit test takes input data, runs it through a piece of code, and checks the result against some pre-defined expected outcome.

Hypothesis does something different. It is a property-based (or: “generative”) testing framework. A property-based test involves defining general expectations about your code instead of specific examples. For example, if you have some code to calculate total VAT on a number of transactions, you could define a bunch of hypothetical numbers with their corresponding VAT amount ($100 transaction → $xx.xx tax) and put this to the test. However, if you know that VAT is, let’s say, 20 percent, a property-based test would verify that total VAT always is 20 percent of the total amount.

Hypothesis is built on these principles. It _generates arbitrary input data according to some specification _and subsequently puts that data to the test. What’s more, when Hypothesis finds an example that causes an assert failure, it’ll try to simplify the example and find the smallest failing case — a process called “shrinking”. Hypothesis will essentially try to “break” your code. Your tests will therefore cover a much larger chunk of your domain space with the same amount of code. And, you’re bound to find edge cases that you hadn’t even thought of.

Getting started with Hypothesis

Let’s see how Hypothesis works in practice. Hypothesis has three key components: the code that you’re testing, the strategies that define your test data, and a function that tests your code using the strategies.

Let’s assume we have a simple (and non-sensical) piece of Python code that converts a float value to an integer:

This code has a clear property: the outcome should always be an integer type.

Strategies

To test this code, we’ll first define a “strategy”. A strategy defines the data that Hypothesis generates for testing, and how examples are “simplified”. In our code, we only define the parameters of the data; the simplification (or: “shrinking”) is internal to Hypothesis.

We’ll start with a strategy that generates a float value between 0.0 and 10.0 (inclusive). We define this in a separate file called data_strategies.py. Using a dataclass for this may seem like overdoing it, but it is useful when you’re working with more complex code that takes a bunch of different parameters.

Using a dataclass and Hypothesis to define your data strategies

A lot of time can go into defining the strategies, and in fact, it should. The whole point of property-based testing with Hypothesis is that you define the parameters from which data is generated, so that you subsequently allow your automated testing to do its magic. The more time you spend on this, the better your testing is bound to be (think: “high investment; high reward”).

python3 data-science coding python programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Data Science With Python Training | Python Data Science Course | Intellipaat

🔵 Intellipaat Data Science with Python course: https://intellipaat.com/python-for-data-science-training/In this Data Science With Python Training video, you...

Python for Data Science | Data Science With Python | Python Data Science Tutorial

🔥Intellipaat Python for Data Science Course: https://intellipaat.com/python-for-data-science-training/In this python for data science video you will learn e...

Applied Data Science with Python Certification Training Course -IgmGuru

Master Applied Data Science with Python and get noticed by the top Hiring Companies with IgmGuru's Data Science with Python Certification Program. Enroll Now

Data Science Course in Dallas

Become a data analysis expert using the R programming language in this [data science](https://360digitmg.com/usa/data-science-using-python-and-r-programming-in-dallas "data science") certification training in Dallas, TX. You will master data...

50 Data Science Jobs That Opened Just Last Week

Data Science and Analytics market evolves to adapt to the constantly changing economic and business environments. Our latest survey report suggests that as the overall Data Science and Analytics market evolves to adapt to the constantly changing economic and business environments, data scientists and AI practitioners should be aware of the skills and tools that the broader community is working on. A good grip in these skills will further help data science enthusiasts to get the best jobs that various industries in their data science functions are offering.