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.
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.
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.
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”).
🔵 Intellipaat Data Science with Python course: https://intellipaat.com/python-for-data-science-training/In this Data Science With Python Training video, you...
🔥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...
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
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...
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.