Skip to content

Commit ca563eb

Browse files
committed
feat: add Python Hypothesis property testing instructions and skill
Adds comprehensive property-based testing guidance for Python with Hypothesis framework: - Instructions file for automatic application to test patterns - Comprehensive skill for on-demand property testing workflows - Covers strategies, stateful testing, migration, debugging, and best practices Follows contribution guidelines and established patterns from existing skills/instructions.
1 parent 3fc2cef commit ca563eb

File tree

4 files changed

+505
-0
lines changed

4 files changed

+505
-0
lines changed

docs/README.instructions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ See [CONTRIBUTING.md](../CONTRIBUTING.md#adding-instructions) for guidelines on
158158
| [PowerShell Pester v5 Testing Guidelines](../instructions/powershell-pester-5.instructions.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fpowershell-pester-5.instructions.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fpowershell-pester-5.instructions.md) | PowerShell Pester testing best practices based on Pester v5 conventions |
159159
| [Project Context](../instructions/moodle.instructions.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fmoodle.instructions.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fmoodle.instructions.md) | Instructions for GitHub Copilot to generate code in a Moodle project context. |
160160
| [Python MCP Server Development](../instructions/python-mcp-server.instructions.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fpython-mcp-server.instructions.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fpython-mcp-server.instructions.md) | Instructions for building Model Context Protocol (MCP) servers using the Python SDK |
161+
| [Python Property Testing with Hypothesis](../instructions/python-property-testing-hypothesis.instructions.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fpython-property-testing-hypothesis.instructions.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fpython-property-testing-hypothesis.instructions.md) | Best practices for property-based testing in Python using Hypothesis framework |
161162
| [Quarkus](../instructions/quarkus.instructions.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fquarkus.instructions.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fquarkus.instructions.md) | Quarkus development standards and instructions |
162163
| [Quarkus MCP Server](../instructions/quarkus-mcp-server-sse.instructions.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fquarkus-mcp-server-sse.instructions.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fquarkus-mcp-server-sse.instructions.md) | Quarkus and MCP Server with HTTP SSE transport development standards and instructions |
163164
| [R Programming Language Instructions](../instructions/r.instructions.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fr.instructions.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fr.instructions.md) | R language and document formats (R, Rmd, Quarto): coding standards and Copilot guidance for idiomatic, safe, and consistent code generation. |

docs/README.skills.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ See [CONTRIBUTING.md](../CONTRIBUTING.md#adding-skills) for guidelines on how to
246246
| [prompt-builder](../skills/prompt-builder/SKILL.md) | Guide users through creating high-quality GitHub Copilot prompts with proper structure, tools, and best practices. | None |
247247
| [publish-to-pages](../skills/publish-to-pages/SKILL.md) | Publish presentations and web content to GitHub Pages. Converts PPTX, PDF, HTML, or Google Slides to a live GitHub Pages URL. Handles repo creation, file conversion, Pages enablement, and returns the live URL. Use when the user wants to publish, deploy, or share a presentation or HTML file via GitHub Pages. | `scripts/convert-pdf.py`<br />`scripts/convert-pptx.py`<br />`scripts/publish.sh` |
248248
| [pytest-coverage](../skills/pytest-coverage/SKILL.md) | Run pytest tests with coverage, discover lines missing coverage, and increase coverage to 100%. | None |
249+
| [python-hypothesis-property-testing](../skills/python-hypothesis-property-testing/SKILL.md) | Generate and optimize property-based tests for Python using Hypothesis framework with strategies, stateful testing, and test migration patterns. | None |
249250
| [python-mcp-server-generator](../skills/python-mcp-server-generator/SKILL.md) | Generate a complete MCP server project in Python with tools, resources, and proper configuration | None |
250251
| [python-pypi-package-builder](../skills/python-pypi-package-builder/SKILL.md) | End-to-end skill for building, testing, linting, versioning, and publishing a production-grade Python library to PyPI. Covers all four build backends (setuptools+setuptools_scm, hatchling, flit, poetry), PEP 440 versioning, semantic versioning, dynamic git-tag versioning, OOP/SOLID design, type hints (PEP 484/526/544/561), Trusted Publishing (OIDC), and the full PyPA packaging flow. Use for: creating Python packages, pip-installable SDKs, CLI tools, framework plugins, pyproject.toml setup, py.typed, setuptools_scm, semver, mypy, pre-commit, GitHub Actions CI/CD, or PyPI publishing. | `references/architecture-patterns.md`<br />`references/ci-publishing.md`<br />`references/community-docs.md`<br />`references/library-patterns.md`<br />`references/pyproject-toml.md`<br />`references/release-governance.md`<br />`references/testing-quality.md`<br />`references/tooling-ruff.md`<br />`references/versioning-strategy.md`<br />`scripts/scaffold.py` |
251252
| [quality-playbook](../skills/quality-playbook/SKILL.md) | Explore any codebase from scratch and generate six quality artifacts: a quality constitution (QUALITY.md), spec-traced functional tests, a code review protocol with regression test generation, an integration testing protocol, a multi-model spec audit (Council of Three), and an AI bootstrap file (AGENTS.md). Includes state machine completeness analysis and missing safeguard detection. Works with any language (Python, Java, Scala, TypeScript, Go, Rust, etc.). Use this skill whenever the user asks to set up a quality playbook, generate functional tests from specifications, create a quality constitution, build testing protocols, audit code against specs, or establish a repeatable quality system for a project. Also trigger when the user mentions 'quality playbook', 'spec audit', 'Council of Three', 'fitness-to-purpose', 'coverage theater', or wants to go beyond basic test generation to build a full quality system grounded in their actual codebase. | `LICENSE.txt`<br />`references/constitution.md`<br />`references/defensive_patterns.md`<br />`references/functional_tests.md`<br />`references/review_protocols.md`<br />`references/schema_mapping.md`<br />`references/spec_audit.md`<br />`references/verification.md` |
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
---
2+
description: 'Best practices for property-based testing in Python using Hypothesis framework'
3+
applyTo: '**/test_*.py, **/*_test.py, **/tests/**/*.py'
4+
---
5+
6+
# Python Property Testing with Hypothesis
7+
8+
Use property-based testing to discover edge cases automatically by testing universal properties instead of specific examples.
9+
10+
## Installation
11+
12+
```bash
13+
pip install "hypothesis[cli]"
14+
```
15+
16+
## Configuration
17+
18+
```python
19+
# conftest.py
20+
from hypothesis import settings
21+
import os
22+
23+
settings.register_profile("dev", max_examples=100)
24+
settings.register_profile("ci", max_examples=1000)
25+
settings.load_profile(os.getenv("HYPOTHESIS_PROFILE", "dev"))
26+
```
27+
28+
```ini
29+
# pytest.ini
30+
[pytest]
31+
markers = property: Property-based tests
32+
hypothesis-show-statistics = true
33+
```
34+
35+
## Common Property Patterns
36+
37+
```python
38+
from hypothesis import given, strategies as st, example
39+
40+
# Idempotency: f(f(x)) == f(x)
41+
@given(st.lists(st.integers()))
42+
def test_reverse_twice(xs):
43+
assert reverse(reverse(xs)) == xs
44+
45+
# Round-trip: decode(encode(x)) == x
46+
@given(st.dictionaries(st.text(), st.integers()))
47+
def test_json_roundtrip(data):
48+
assert json.loads(json.dumps(data)) == data
49+
50+
# Invariant: property always holds
51+
@given(st.lists(st.integers(), min_size=1))
52+
def test_max_in_list(xs):
53+
assert max(xs) in xs
54+
55+
# Oracle: compare with known implementation
56+
@given(st.lists(st.integers()))
57+
def test_sort_matches_builtin(xs):
58+
assert custom_sort(xs) == sorted(xs)
59+
60+
# Commutativity: f(a, b) == f(b, a)
61+
@given(st.integers(), st.integers())
62+
def test_addition_commutative(a, b):
63+
assert a + b == b + a
64+
```
65+
66+
## Strategies
67+
68+
```python
69+
# Built-in strategies
70+
st.integers(min_value=0, max_value=100)
71+
st.text(min_size=1, max_size=100)
72+
st.lists(st.integers(), max_size=50)
73+
st.dictionaries(st.text(), st.integers())
74+
st.dates(min_value=date(2000, 1, 1))
75+
st.emails()
76+
77+
# Composite strategies
78+
from hypothesis.strategies import composite
79+
80+
@composite
81+
def user_strategy(draw):
82+
return User(
83+
username=draw(st.text(min_size=3)),
84+
age=draw(st.integers(min_value=18, max_value=120)),
85+
email=draw(st.emails())
86+
)
87+
```
88+
89+
## Stateful Testing
90+
91+
```python
92+
from hypothesis.stateful import RuleBasedStateMachine, rule, invariant
93+
94+
class StackMachine(RuleBasedStateMachine):
95+
def __init__(self):
96+
super().__init__()
97+
self.stack = []
98+
99+
@rule(value=st.integers())
100+
def push(self, value):
101+
self.stack.append(value)
102+
103+
@rule()
104+
def pop(self):
105+
if self.stack:
106+
self.stack.pop()
107+
108+
@invariant()
109+
def size_non_negative(self):
110+
assert len(self.stack) >= 0
111+
112+
TestStack = StackMachine.TestCase
113+
```
114+
115+
## Migration from Example Tests
116+
117+
```python
118+
# Keep critical edge cases
119+
@given(st.lists(st.integers()))
120+
@example([]) # Empty
121+
@example([1]) # Single
122+
@example([1, 1, 1]) # Duplicates
123+
def test_sort(xs):
124+
result = sort(xs)
125+
assert sorted(result) == sorted(xs)
126+
```
127+
128+
## Debugging
129+
130+
```python
131+
from hypothesis import seed, note
132+
133+
# Reproduce failure
134+
@seed(1234567890) # From failure output
135+
@given(st.integers())
136+
def test_reproduce(x):
137+
note(f"Input: {x}")
138+
assert process(x) is not None
139+
```
140+
141+
## Settings
142+
143+
```python
144+
from hypothesis import settings
145+
from datetime import timedelta
146+
147+
@settings(
148+
max_examples=50,
149+
deadline=timedelta(milliseconds=100)
150+
)
151+
@given(st.lists(st.integers()))
152+
def test_with_settings(xs):
153+
assert process(xs) is not None
154+
```
155+
156+
## Best Practices
157+
158+
**DO:**
159+
- Test universal properties, not specific examples
160+
- Use strategy constraints over `assume()`
161+
- Keep critical edge cases with `@example()`
162+
- Run CI with `max_examples=1000`
163+
164+
**AVOID:**
165+
- Test implementation details
166+
- Use `random.random()` (breaks reproducibility)
167+
- Over-use `assume()` (prefer constraints)
168+
169+
## Common Patterns
170+
171+
```python
172+
# Prefer strategy constraints
173+
@given(st.integers().map(lambda x: x * 2))
174+
def test_even(n):
175+
assert n % 2 == 0
176+
177+
# Avoid heavy filtering
178+
@given(st.integers())
179+
def test_even_bad(n):
180+
assume(n % 2 == 0) # Not recommended
181+
```
182+
183+
## Resources
184+
185+
- [Hypothesis Documentation](https://hypothesis.readthedocs.io/)
186+
- [Property-Based Testing Guide](https://fsharpforfunandprofit.com/posts/property-based-testing/)

0 commit comments

Comments
 (0)