Skip to content

Commit 9cbb7df

Browse files
committed
Add failing test for demonstration purpose
1 parent 9be4365 commit 9cbb7df

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

source-code/testing/Hypothesis/factorial.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,22 @@ def fac(n: int) -> int:
2525
for i in range(n, 1, -1):
2626
result *= i
2727
return result
28+
29+
30+
def fac_bad(n: int) -> int:
31+
'''Return the factorial of n
32+
33+
Parameters
34+
----------
35+
n : int
36+
The number to compute the factorial of
37+
38+
Returns
39+
-------
40+
int
41+
The factorial of n
42+
'''
43+
result = 1
44+
for i in range(n, 1, -1):
45+
result *= i
46+
return result
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
from math import factorial as fac_math
22
from hypothesis import assume, given, strategies as st
3-
from factorial import fac
3+
from factorial import fac, fac_bad
44

55

66
@given(st.integers(min_value=0, max_value=100))
7-
def test_fac_same(n):
7+
def test_fac(n):
88
assert fac(n) == fac_math(n)
99

1010

1111
@given(st.integers(max_value=-1))
1212
def test_fac_neg(n):
1313
try:
14-
assert fac(n) == fac_math(n)
14+
_ = fac(n)
15+
assert False, 'no ValueError raised'
16+
except ValueError:
17+
assert n < 0
18+
19+
20+
@given(st.integers(min_value=0, max_value=100))
21+
def test_fac_bad(n):
22+
assert fac_bad(n) == fac_math(n)
23+
24+
25+
@given(st.integers(max_value=-1))
26+
def test_fac_bad_neg(n):
27+
try:
28+
_ = fac_bad(n)
29+
assert False, 'no ValueError raised'
1530
except ValueError:
1631
assert n < 0

0 commit comments

Comments
 (0)