Skip to content

Commit 8fd1426

Browse files
committed
Add Hypothesis support for property-based testing in evaluation function
Updates dependency list (Poetry and `pyproject.toml`) to include `hypothesis` and adds unit tests demonstrating property-based test evaluation.
1 parent 5885213 commit 8fd1426

3 files changed

Lines changed: 71 additions & 3 deletions

File tree

evaluation_function/evaluation_test.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ def test_run_code_captures_images(self):
134134
" self.assertEqual(square(0), 0)\n"
135135
)
136136

137+
_SQUARE_TESTS_HYPOTHESIS = (
138+
"from hypothesis import given, settings\n"
139+
"import hypothesis.strategies as st\n"
140+
"@given(st.integers(-100, 100))\n"
141+
"@settings(max_examples=50)\n"
142+
"def test_square(n):\n"
143+
" assert square(n) == n * n, f'square({n}) = {square(n)}, expected {n*n}'\n"
144+
)
145+
137146

138147
def _unit_params(test_code):
139148
return {"mode": "unit_test", "test_code": test_code}
@@ -180,4 +189,17 @@ def test_student_print_no_pollution(self):
180189
result = evaluation_function(code_with_print, None, _unit_params(_SQUARE_TESTS)).to_dict()
181190

182191
self.assertTrue(result["is_correct"])
183-
self.assertIn("2/2 tests passed", result["feedback"])
192+
self.assertIn("2/2 tests passed", result["feedback"])
193+
194+
def test_hypothesis_pass(self):
195+
result = evaluation_function(_SQUARE_FN, None, _unit_params(_SQUARE_TESTS_HYPOTHESIS)).to_dict()
196+
197+
self.assertTrue(result["is_correct"])
198+
self.assertIn("1/1 tests passed", result["feedback"])
199+
200+
def test_hypothesis_fail_shows_minimal_example(self):
201+
result = evaluation_function(_WRONG_SQUARE_FN, None, _unit_params(_SQUARE_TESTS_HYPOTHESIS)).to_dict()
202+
203+
self.assertFalse(result["is_correct"])
204+
self.assertIn("0/1 tests passed", result["feedback"])
205+
self.assertIn("square(", result["feedback"])

poetry.lock

Lines changed: 47 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ statsmodels = ">=0.14"
2525
requests = ">=2.32"
2626
boto3 = ">=1.42"
2727
python-dotenv = ">=1.0"
28+
hypothesis = "^6.152.9"
2829

2930
[tool.poetry.group.dev.dependencies]
3031
pytest = "^8.2.2"

0 commit comments

Comments
 (0)