Python Zero to Heros

Online Absolute Beginner Python Tutorials 

Every Sunday 2pm (UK time/ BST)

Recap

Python objects - int, float, str, list, dict, bool

Control flows - if-else, for loop, while loop

Functions, modeuls, classes and decorators

strings operations and regex with re

pytest with fixtures and mock

 

Any Questions?

Property-based Testing

Parametrize : thing of multiple cases that you want to run your test against

 

Proptery-based : randomly generate test cases witin the range of the parameter

Property-based Testing

Origaniated as QuickCheck  in 1999

 

In QuickCheck, assertions are written about logical properties that a function should fulfill. Then QuickCheck attempts to generate a test case that falsifies such assertions. Once such a test case is found, QuickCheck tries to reduce it to a minimal failing subset by removing or simplifying input data that are unneeded to make the test fail.

- wikipeida

Hypothesis

Hypothesis runs your tests against a much wider range of scenarios than a human tester could, finding edge cases in your code that you would otherwise have missed. It then turns them into simple and easy to understand failures that save you time and money compared to fixing them if they slipped through the cracks and a user had run into them instead.

- hypothesis.works

Let's get started

Installation

pip install hypothesis

Optionally you can install extentions which include other popular objects form popular libraries e.g.

pip install hypothesis[pandas,django]

Usage Example

def encode(input_string):
    count = 1
    prev = ""
    lst = []
    for character in input_string:
        if character != prev:
            if prev:
                entry = (prev, count)
                lst.append(entry)
            count = 1
            prev = character
        else:
            count += 1
    entry = (character, count)
    lst.append(entry)
    return lst


def decode(lst):
    q = ""
    for character, count in lst:
        q += character * count
    return q

Usage Example

from hypothesis import given
from hypothesis.strategies import text


@given(text())
def test_decode_inverts_encode(s):
    assert decode(encode(s)) == s

Usage Example

def encode(input_string):
    count = 1
    prev = ""
    lst = []
    for character in input_string:
        if character != prev:
            if prev:
                entry = (prev, count)
                lst.append(entry)
            # count = 1  # Missing reset operation
            prev = character
        else:
            count += 1
    entry = (character, count)
    lst.append(entry)
    return lst

Different strategies

Other cases that this would be useful

 

Parsing data
(e.g. dateutil.parser)

 

Data cleaning/ preprocessing algrithm in ML
(e.g. sklearn.preprocessing)

 

Scientific calculations

(e.g. scipy)

Let's try to write some tests

Next week:
linting your code

Sunday 2pm (UK time/ BST)

There are also Mid Meet Py every Wednesday 1pm

Testing month in June