Intro to property-based testing in Python

329 阅读1分钟

Intro to property-based testing in Python

In this article we will learn a unique and effective approach to testing called property-based testing. We will use Python , pytest and Hypothesis to implement this testing approach.

The article is going to use basic pytest concepts to explain property-based testing. I recommend that you read this article to quickly brush up your pytest knowledge.

We will start with the conventional unit/functional testing method known as example-based testing which most of us use. We try to find its shortcomings, and then move to the property-based approach to remove those shortcomings.

Every great magic trick consists of three parts or acts. The first part is called “The Pledge”. The magician shows you something ordinary: a deck of cards, a bird or a man. He shows you this object. Perhaps he asks you to inspect it to see if it is indeed real, unaltered, normal. But of course…it probably isn’t.

Part 1: Example-based testing

The approach of example-based testing has the following steps:

  • given a test input I
  • when passed to function under test
  • should return an output O

So, basically we give a fixed input and expect a fixed output.

To understand this concept in layman’s terms:

A machine under test

Assume we have a machine which takes plastic of any shape and any colour as input and produces a perfectly round plastic ball of the same colour as output.

Photo by Greyson Joralemon on Unsplash

Now, to test this machine using example-based testing, we will follow below approach:

  1. take a blue-coloured raw plastic (fixed test data)
  2. feed the plastic to machine
  3. expect a blue-coloured plastic ball as output(fixed test output)

Let’s see the same approach in a programmatic way.

Prerequisite: make sure you have Python (ver 2.7 or above) and pytest installed.

Create a directory structure like this:

- demo_tests/    - test_example.py

We will write one small function sum inside file test_example.py . This accepts two numbers — num1 and num2 — as parameters and returns the addition of both numbers as result.

def sum(num1, num2):    """It returns sum of two numbers"""    return num1 + num2

Now, lets write a test to test this sum function following the conventional method.

import pytest
#make sure to start function name with testdef test_sum():    assert sum(1, 2) == 3

Here you can see that, we are passing the two values 1 and 2 and expecting the sum to return 3.

Run the tests by traversing to demo_tests folder and then running following command:

pytest test_example.py -v