### Cheuk Ting Ho

Developer advocate / Data Scientist - support open-source and building the community.

Cheuk Ting Ho

- Importing the code

Â - Organizing the tests

Â - Think of test cases

- the given is obvious

Â - works extra well with typing

Â - edge case automatically be found

- need to think of what is and what is not

Â - take extra steps to write examples

Â - may overlook edge cases

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
```

```
from hypothesis import given
from hypothesis.strategies import text
@given(text())
def test_decode_inverts_encode(s):
assert decode(encode(s)) == s
```

- Have to install the extra like this:

`pip install hypothesis[numpy]`

Â - located at `hypothesis.extra.numpy`

Â - Provide strategies for scalar and array dtypes

Â - https://hypothesis.readthedocs.io/en/latest/numpy.html

- Also need to install the extra like this:

`pip install hypothesis[numpy]`

Â - located at `hypothesis.extra.pandas`

Â - Provide strategies for pd.Index, pd.Series and pd.DataFrame

Â - https://hypothesis.readthedocs.io/en/latest/numpy.html

But I hope you are still using typing ðŸ™ƒ

- Using the typing as a hint

Â - pick the right strategy for input parameters

Â - CLI tool included

Â - Automatically `black` the code for you

`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)
```

Let's try ðŸ˜‰

- tests can run much slower

(generate strategies are expansive)

Â - tests can be harder to understand

Â - if no typing not much can be done

(don't be too lazy ðŸ‘»)

By Cheuk Ting Ho

- 150

Developer advocate / Data Scientist - support open-source and building the community.