Cheuk Ting Ho
Developer advocate / Data Scientist - support open-source and building the community.
Online Absolute Beginner Python Tutorials
Every Sunday 2pm (UK time/ BST)
by Cheuk Ting Ho
Get this slide deck: slides.com/cheukting_ho/python-property-based-testing
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
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
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 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.
Let's get started
pip install hypothesis
Optionally you can install extentions which include other popular objects form popular libraries e.g.
pip install hypothesis[pandas,django]
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
from hypothesis import given
from hypothesis.strategies import text
@given(text())
def test_decode_inverts_encode(s):
assert decode(encode(s)) == s
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
Parsing data
(e.g. dateutil.parser)
Data cleaning/ preprocessing algrithm in ML
(e.g. sklearn.preprocessing)
Scientific calculations
(e.g. scipy)
Sunday 2pm (UK time/ BST)
There are also Mid Meet Py every Wednesday 1pm
Testing month in June
By Cheuk Ting Ho
Developer advocate / Data Scientist - support open-source and building the community.