-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from ssciwr/add_hypothesis_property_testing_ex…
…ample Add property testing example using hypothesis library
- Loading branch information
Showing
3 changed files
with
48 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from __future__ import annotations | ||
from effective_software_testing.board import Board | ||
from hypothesis import given, assume, example | ||
import hypothesis.strategies as st | ||
|
||
|
||
# sample n from integers in the range 0 <= n <= 99 | ||
@given(n=st.integers(min_value=0, max_value=99)) | ||
@example(n=3) # always include n=3 | ||
def test_property_empty_board_game_not_over(n: int) -> None: | ||
board = Board(n) | ||
assert board.n == n | ||
assert board.game_is_over is False | ||
|
||
|
||
@given(n=st.integers(1, 12), x=st.integers(0, 11), y=st.integers(0, 11)) | ||
def test_property_empty_board_is_empty_with_assume(n: int, x: int, y: int) -> None: | ||
assume(x < n) # if x >= n we re-sample n,x,y | ||
assume(y < n) | ||
# assume works but quickly gets inefficient if many inputs do not satisfy our assumptions | ||
board = Board(n) | ||
assert board.square(x, y) is None | ||
|
||
|
||
# using composite is a more efficient way to sample from our desired distribution of values | ||
@st.composite | ||
def sample_valid_n_x_y(draw: st.DrawFn) -> tuple[int, int, int]: | ||
max_board_n = 12 | ||
n = draw(st.integers(1, max_board_n)) | ||
x = draw(st.integers(0, n - 1)) | ||
y = draw(st.integers(0, n - 1)) | ||
return n, x, y | ||
|
||
|
||
@given(n_x_y=sample_valid_n_x_y()) | ||
def test_property_empty_board_is_empty_with_composite( | ||
n_x_y: tuple[int, int, int] | ||
) -> None: | ||
# no assume required here as all the x,y we generate are < n | ||
n, x, y = n_x_y | ||
board = Board(n) | ||
assert board.square(x, y) is None |