forked from kallimachos/cookbook
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
executable file
·90 lines (64 loc) · 1.31 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/python3
"""
Pytest and hypothesis examples.
Run using 'pytest test.py'
"""
import pytest
from hypothesis import given
from hypothesis.strategies import integers, text
# Functions to be tested.
def add(x):
"""
Return x +1.
>>> add(2)
3
"""
return x + 1
def square(x):
"""
Square x.
>>> square(2)
4
>>> square(-2)
4
"""
return x * x
def mystring(s):
"""Return s."""
return s + s
# Test using hypothesis data generation
@given(integers())
def test_add_h(i):
"""Test add."""
assert add(i) == i + 1
@given(integers())
def test_square_h(i):
"""Test square."""
assert square(i) == i * i
@given(text())
def test_mystring_h(s):
"""Test string."""
x = mystring(s)
assert x == s + s
# Test using pytest parameters
@pytest.mark.parametrize(
"value, result",
[(1, 2), (2, 3), (3, 4)],
)
def test_add_p(value, result):
"""Test add."""
assert add(value) == result
@pytest.mark.parametrize(
"value, result",
[(1, 1), (2, 4), (3, 9)],
)
def test_square_p(value, result):
"""Test add."""
assert square(value) == result
@pytest.mark.parametrize(
"value, result",
[("hi", "hihi"), ("bye", "byebye")],
)
def test_mystring_p(value, result):
"""Test add."""
assert mystring(value) == result