Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add property testing example using hypothesis library #12

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- name: Checkout repository
Expand All @@ -29,11 +29,9 @@ jobs:
# setuptools_scm requires a non-shallow clone of the repository
fetch-depth: 0

- name: Linux setup
if: runner.os == 'Linux'
# qt requires some system libraries on linux that are not installed by default
run: |
sudo apt-get update -yy && sudo apt-get install -yy libegl1 libxcb-icccm4 libxcb-image0 libxcb-keysyms1 libxcb-randr0 libxcb-render-util0 libxcb-xinerama0 libxcb-xfixes0 x11-utils libxkbcommon-x11-0
# qt requires some system libraries on linux that are not installed by default
- name: Install system libs required by Qt on linux
uses: tlambert03/setup-qt-libs@v1

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ tests = [
"pytest-randomly",
"pytest-qt",
"pytest-xvfb",
"ipytest"
"ipytest",
"hypothesis",
]

# Command line scripts installed as part of the installation
Expand Down
42 changes: 42 additions & 0 deletions tests/test_board_property.py
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