Cheuk Ting Ho
Developer advocate / Data Scientist - support open-source and building the community.
https://slides.com/cheukting_ho/writing-tests-use-hypothesis
Can be used with pytest or unitest
entry point to modify the test
generating test data
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
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
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
from hypothesis import given from hypothesis.strategies import text @given(text()) def test_decode_inverts_encode(s): assert decode(encode(s)) == s
from hypothesis import given from hypothesis.strategies import text @given(text()) def test_decode_inverts_encode(s): assert decode(encode(s)) == s
hypothesis write gzip
Just type in command line...
from re import compile, error from hypothesis.extra import ghostwriter ghostwriter.fuzz(compile, except_=error)
from typing import Sequence from hypothesis.extra import ghostwriter def timsort(seq: Sequence[int]) -> Sequence[int]: return sorted(seq) ghostwriter.idempotent(timsort)
import json from hypothesis.extra import ghostwriter ghostwriter.roundtrip(json.dumps, json.loads)
import math from hypothesis.extra import ghostwriter def my_pow(x, y): result = 1.0 for _ in range(y): result *= x return result ghostwriter.equivalent(my_pow, math.pow)
https://slides.com/cheukting_ho/writing-tests-use-hypothesis
By Cheuk Ting Ho
Developer advocate / Data Scientist - support open-source and building the community.