mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 23:04:59 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+236
-149
@@ -1,164 +1,251 @@
|
||||
---
|
||||
description: Comprehensive guide covering best practices for the Hypothesis Python library, including coding standards, testing, performance, and security. Provides actionable guidance for developers to write maintainable, robust, and efficient property-based tests.
|
||||
globs: *.py
|
||||
description: Definitive guidelines for writing robust, maintainable, and performant property-based tests using Hypothesis with Python and pytest.
|
||||
globs: **/*.py
|
||||
---
|
||||
# Hypothesis Library Best Practices and Coding Standards
|
||||
# hypothesis Best Practices
|
||||
|
||||
This document provides comprehensive guidelines for using the Hypothesis Python library effectively. Following these best practices will lead to more maintainable, robust, and efficient property-based tests.
|
||||
Hypothesis is a powerful property-based testing library. These guidelines ensure your Hypothesis tests are effective, maintainable, and integrate seamlessly into modern Python development workflows.
|
||||
|
||||
## I. General Python Coding Standards (Applicable to Hypothesis Projects)
|
||||
## 1. Code Organization and Structure
|
||||
|
||||
These are general Python coding standards that should be adhered to in any Python project, including those using Hypothesis.
|
||||
Always integrate Hypothesis with `pytest`. Keep test files in a dedicated `tests/` directory, mirroring your `src/` structure. Adhere to PEP 8, use clear docstrings, and apply type hints.
|
||||
|
||||
* **PEP 8 Compliance:** Adhere strictly to PEP 8 style guidelines for Python code. This includes:
|
||||
* Indentation: Use 4 spaces per indentation level.
|
||||
* Line Length: Limit lines to a maximum of 79 characters (or 72 for comments and docstrings).
|
||||
* Blank Lines: Use blank lines to separate top-level function and class definitions (two lines) and method definitions inside a class (one line).
|
||||
* Imports: Group imports into standard library, third-party, and local application/library-specific imports, separated by blank lines.
|
||||
* Whitespace: Avoid extraneous whitespace.
|
||||
* Naming Conventions: Follow appropriate naming conventions for variables, functions, classes, and constants.
|
||||
* **PEP 257 Compliance:** Follow PEP 257 for docstring conventions.
|
||||
* Write docstrings for all public modules, functions, classes, and methods.
|
||||
* Use triple double quotes (`"""`) for docstrings.
|
||||
* Include a summary line, use case (if appropriate), arguments, return type and semantics (unless None is returned), and examples in multiline docstrings.
|
||||
* **Use Python 3.12+:** Ensure that your project uses a modern version of Python (3.12 or higher) for access to the latest features and security updates.
|
||||
* **Virtual Environments:** Use virtual environments (e.g., `venv` or `virtualenv`) to isolate project dependencies.
|
||||
* **Dependency Management:** Use a dependency management tool like `pip` with a `requirements.txt` or `pyproject.toml` file to specify project dependencies. It's recommended to use a modern package installer such as [UV](https://astral.sh/blog/uv) for faster and more reproducible builds.
|
||||
* **Code Reviews:** Conduct regular code reviews to ensure code quality and adherence to coding standards.
|
||||
* **Type Hinting:** Use type hints to improve code readability and enable static analysis.
|
||||
* **Linters and Formatters:** Use linters (e.g., `flake8`, `pylint`) and formatters (e.g., `black`, `autopep8`) to automatically enforce coding standards.
|
||||
* **Error Handling:** Implement robust error handling using `try...except` blocks.
|
||||
* **Logging:** Use the `logging` module for structured logging.
|
||||
* **Clear and Concise Code:** Write code that is easy to understand and maintain.
|
||||
* **Modularity:** Design your code with a modular architecture.
|
||||
* **Documentation:** Write clear and comprehensive documentation.
|
||||
* **Follow SOLID principles** When designing code and modules.
|
||||
❌ BAD: Generic test function names, no `st` alias, mixed with application code.
|
||||
```python
|
||||
# my_app/utils.py
|
||||
from hypothesis import given, strategies
|
||||
def add(a, b): return a + b
|
||||
|
||||
## II. Hypothesis-Specific Best Practices
|
||||
@given(strategies.integers(), strategies.integers())
|
||||
def test_add(a, b):
|
||||
assert add(a, b) == a + b
|
||||
```
|
||||
|
||||
These guidelines are specifically tailored to the Hypothesis library for property-based testing.
|
||||
✅ GOOD: Dedicated test files, `st` alias, clear naming, `pytest` integration.
|
||||
```python
|
||||
# src/my_app/utils.py
|
||||
def add(a: int, b: int) -> int:
|
||||
"""Adds two numbers."""
|
||||
return a + b
|
||||
|
||||
### A. Writing Properties
|
||||
|
||||
* **Focus on Properties, Not Examples:** Hypothesis excels when you define *properties* that should always hold true for a wide range of inputs, rather than just providing a fixed set of examples. A property describes a general relationship that should be invariant.
|
||||
* **Define Strategies Clearly:** Strategies are the core of Hypothesis. Define strategies that generate diverse and representative data for your properties. Hypothesis provides many built-in strategies, and you can combine and customize them.
|
||||
* **Shrinking is Crucial:** Hypothesis shrinks failing examples to the smallest and simplest form that still causes the failure. This simplifies debugging. Ensure your strategies are designed to allow effective shrinking.
|
||||
* **Avoid Direct Assertion:** Generally, avoid directly asserting specific values within your properties. Instead, focus on asserting relationships between inputs and outputs. This strengthens the test and covers more potential cases.
|
||||
* **Use Assume Sparingly:** The `assume()` function tells Hypothesis to discard certain examples. Overuse of `assume()` can make your tests less effective and potentially mask bugs. Try to refine your strategies instead of filtering excessively.
|
||||
* **Make Properties Declarative:** Properties should describe *what* should happen, not *how* it should happen. This makes the tests more robust and easier to understand.
|
||||
* **Test Invariants, Not Implementations:** Properties should test the high-level invariants of your code, rather than implementation details. This makes the tests more resilient to code changes.
|
||||
* **Minimize Side Effects:** Properties should be as pure as possible, with minimal side effects. This makes the tests more predictable and easier to debug.
|
||||
* **Consider Edge Cases:** Think about edge cases and boundary conditions that might violate your properties. Design your strategies to include these cases.
|
||||
* **Use Composite Strategies:** Composite strategies allow you to combine multiple strategies to generate complex data structures. Use them to create realistic and diverse test cases.
|
||||
* **Label Strategies:** Label strategies with descriptive names to improve the readability of your test output.
|
||||
* **Use Dataclasses for Structured Data:** Define dataclasses to represent structured data in your properties. This improves code readability and maintainability.
|
||||
|
||||
### B. Strategy Design
|
||||
|
||||
* **Start with Built-in Strategies:** Hypothesis provides a rich set of built-in strategies for generating common data types. Start with these strategies and customize them as needed.
|
||||
* **Constrain Strategies Appropriately:** Use constraints (e.g., `min_value`, `max_value`, `length`) to narrow the range of values generated by your strategies and improve test performance.
|
||||
* **Use `sampled_from` for Discrete Values:** The `sampled_from` strategy is useful for generating discrete values from a list or set.
|
||||
* **Combine Strategies with `one_of`:** The `one_of` strategy allows you to combine multiple strategies to generate a variety of data types.
|
||||
* **Use `lists`, `sets`, and `dictionaries` to Generate Collections:** These strategies generate collections of data. Specify the element type and size constraints as needed.
|
||||
* **Consider `recursive` Strategies for Nested Structures:** The `recursive` strategy allows you to generate nested data structures, such as trees or graphs.
|
||||
* **Write Custom Strategies When Necessary:** If the built-in strategies are not sufficient, you can write custom strategies to generate data specific to your application.
|
||||
* **Use `register_type_strategy` for Custom Types:** Register custom strategies for your custom types to make them available to other strategies.
|
||||
* **Think About Shrinking When Designing Strategies:** Design your strategies with shrinking in mind. The goal is to shrink failing examples to the smallest and simplest form possible.
|
||||
|
||||
### C. Code Organization
|
||||
|
||||
* **Separate Tests from Implementation:** Keep your hypothesis tests in a separate directory from your implementation code (e.g., `tests/`).
|
||||
* **Organize Tests by Module:** Mirror the structure of your implementation code in your test directory. Create a test module for each implementation module.
|
||||
* **Use Descriptive Test Names:** Give your tests descriptive names that indicate the property being tested.
|
||||
* **Use Fixtures for Setup and Teardown:** Use pytest fixtures to handle setup and teardown tasks for your tests.
|
||||
* **Create Helper Functions for Common Tasks:** Create helper functions to encapsulate common tasks in your tests.
|
||||
* **Use a `conftest.py` file:** Use a `conftest.py` file to define fixtures and other configuration options that are shared across multiple test modules.
|
||||
* **Keep Test Modules Small:** Break large test modules into smaller, more manageable modules.
|
||||
|
||||
### D. Error Handling
|
||||
|
||||
* **Test for Expected Exceptions:** Use `pytest.raises` to assert that your code raises the expected exceptions under certain conditions.
|
||||
* **Use Context Managers for Exception Handling:** Use context managers to handle exceptions and ensure that resources are properly released.
|
||||
* **Log Errors and Warnings:** Use the `logging` module to log errors and warnings in your tests.
|
||||
* **Use `report` to Add Context to Failures:** Use `hypothesis.report` to add information about the current state of the system under test to the failure report.
|
||||
* **Fail Fast:** Write tests that fail quickly when a property is violated.
|
||||
|
||||
### E. Performance Considerations
|
||||
|
||||
* **Minimize Computation in Properties:** Properties should be as lightweight as possible to avoid slowing down the tests.
|
||||
* **Use Caching to Avoid Redundant Computations:** Use caching to avoid redundant computations in your properties.
|
||||
* **Optimize Strategies for Performance:** Optimize your strategies to generate data efficiently.
|
||||
* **Use `max_examples` to Limit Test Run Time:** Use the `max_examples` setting to limit the number of examples generated by Hypothesis and prevent tests from running indefinitely.
|
||||
* **Profile Slow Tests:** Use profiling tools to identify slow tests and optimize their performance.
|
||||
* **Consider Data Generation Overhead:** Be aware that complex strategy definitions can have a significant performance overhead.
|
||||
* **Avoid Unnecessary `assume` Calls:** Excessive use of `assume` can lead to wasted test cycles.
|
||||
|
||||
### F. Security Best Practices
|
||||
|
||||
* **Test for Input Validation:** Use Hypothesis to test the input validation logic of your code. Generate a wide range of inputs, including invalid inputs, to ensure that your code handles them correctly.
|
||||
* **Test for Common Vulnerabilities:** Use Hypothesis to test for common vulnerabilities, such as SQL injection, cross-site scripting (XSS), and buffer overflows.
|
||||
* **Use Strategies to Generate Malicious Inputs:** Create strategies that generate malicious inputs to test the security of your code.
|
||||
* **Sanitize Inputs:** Sanitize all inputs to prevent vulnerabilities.
|
||||
* **Escape Outputs:** Escape all outputs to prevent vulnerabilities.
|
||||
* **Use Secure API Communication:** Use HTTPS for secure API communication.
|
||||
* **Implement Authentication and Authorization:** Implement authentication and authorization to protect your data.
|
||||
* **Limit User Privileges:** Limit user privileges to the minimum necessary to perform their tasks.
|
||||
* **Monitor Your Application for Security Threats:** Monitor your application for security threats and respond promptly to any incidents.
|
||||
|
||||
### G. Common Pitfalls and Gotchas
|
||||
|
||||
* **Overuse of `assume()`:** As mentioned before, overuse of `assume()` can hide bugs.
|
||||
* **Complex Strategy Definitions:** Complex strategy definitions can be difficult to understand and maintain.
|
||||
* **Unrealistic Test Data:** Generating unrealistic test data can lead to false positives or false negatives.
|
||||
* **Slow Test Execution:** Slow test execution can make it difficult to iterate quickly.
|
||||
* **Lack of Understanding of Shrinking:** A lack of understanding of shrinking can make it difficult to debug failing tests.
|
||||
* **Not Testing Edge Cases:** Failing to test edge cases can lead to bugs in production.
|
||||
* **Ignoring Hypothesis Output:** Ignoring the Hypothesis output can cause you to miss important information about failing tests.
|
||||
* **Incorrect Type Annotations:** Incorrect type annotations can cause Hypothesis to generate incorrect data.
|
||||
* **Misunderstanding of Strategy Composition:** Misunderstanding how strategies are composed can lead to unexpected results.
|
||||
|
||||
### H. Tooling and Environment
|
||||
|
||||
* **pytest:** Use pytest as your test runner. It integrates seamlessly with Hypothesis and provides a rich set of features for writing and running tests.
|
||||
* **hypothesis-auto:** Use the `hypothesis-auto` extension to automatically generate strategies for your dataclasses and other data types.
|
||||
* **black:** Use black to automatically format your code and ensure PEP 8 compliance.
|
||||
* **flake8:** Use flake8 to lint your code and identify potential problems.
|
||||
* **mypy:** Use mypy to statically type check your code and identify type errors.
|
||||
* **tox:** Use tox to automate testing in multiple environments.
|
||||
* **CI/CD:** Integrate your tests into your CI/CD pipeline to ensure that your code is always tested before it is deployed.
|
||||
* **VS Code or PyCharm:** Use a good IDE (like VS Code with the Python extension, or PyCharm) for code editing, debugging, and testing.
|
||||
|
||||
### I. Testing Approaches
|
||||
|
||||
* **Unit Testing:** Focus on testing individual units of code in isolation.
|
||||
* **Integration Testing:** Test how different units of code interact with each other.
|
||||
* **End-to-End Testing:** Test the entire application from end to end.
|
||||
* **Property-Based Testing:** Use Hypothesis to generate a wide range of inputs and test the properties of your code.
|
||||
* **Mutation Testing:** Use mutation testing to assess the quality of your tests.
|
||||
|
||||
### J. Recommended Workflow
|
||||
|
||||
1. **Start with a Property:** Define the property that you want to test.
|
||||
2. **Define a Strategy:** Create a strategy to generate data for your property.
|
||||
3. **Write the Test:** Write the test using Hypothesis and pytest.
|
||||
4. **Run the Test:** Run the test and see if it passes.
|
||||
5. **Fix Any Bugs:** If the test fails, fix the bug in your code.
|
||||
6. **Repeat:** Repeat steps 1-5 until you are confident that your code is correct.
|
||||
|
||||
## III. Example Hypothesis Test
|
||||
|
||||
python
|
||||
import pytest
|
||||
# tests/test_utils.py
|
||||
from hypothesis import given, strategies as st
|
||||
|
||||
def add(x, y):
|
||||
return x + y
|
||||
import pytest
|
||||
from src.my_app.utils import add
|
||||
|
||||
@given(st.integers(), st.integers())
|
||||
def test_addition_is_commutative(x, y):
|
||||
assert add(x, y) == add(y, x)
|
||||
def test_add_is_commutative(a: int, b: int) -> None:
|
||||
"""Verify addition is commutative."""
|
||||
assert add(a, b) == add(b, a)
|
||||
|
||||
@given(st.integers())
|
||||
def test_add_identity_element(a: int) -> None:
|
||||
"""Verify zero is the identity element for addition."""
|
||||
assert add(a, 0) == a
|
||||
```
|
||||
|
||||
## IV. Conclusion
|
||||
## 2. Strategy Selection and Constraints
|
||||
|
||||
By following these best practices, you can use Hypothesis to write more effective and robust property-based tests that improve the quality and reliability of your Python code.
|
||||
Always use the most specific and constrained strategies possible. This improves performance and focuses generated examples on relevant edge cases. Use `st.composite` for dependent generation.
|
||||
|
||||
❌ BAD: Overly broad strategies or excessive filtering with `assume()` for basic constraints.
|
||||
```python
|
||||
@given(st.integers(), st.integers())
|
||||
def test_division(numerator, denominator):
|
||||
assume(denominator != 0) # Discards many examples
|
||||
assume(numerator % denominator == 0) # Discards even more
|
||||
assert numerator / denominator == numerator // denominator
|
||||
```
|
||||
|
||||
✅ GOOD: Constrain strategies directly. Use `st.composite` for dependent values.
|
||||
```python
|
||||
from hypothesis import given, strategies as st, assume
|
||||
from hypothesis.extra.math import non_zero_floats
|
||||
from typing import Tuple
|
||||
|
||||
@given(st.integers(), st.integers(min_value=1, max_value=100))
|
||||
def test_division_positive_divisor(numerator: int, divisor: int) -> None:
|
||||
"""Test integer division with a positive divisor."""
|
||||
assert (numerator // divisor) * divisor + (numerator % divisor) == numerator
|
||||
|
||||
@st.composite
|
||||
def non_zero_pairs(draw) -> Tuple[int, int]:
|
||||
"""Generates a pair of integers where the second is non-zero."""
|
||||
numerator = draw(st.integers())
|
||||
denominator = draw(st.integers().filter(lambda d: d != 0))
|
||||
return numerator, denominator
|
||||
|
||||
@given(non_zero_pairs())
|
||||
def test_division_any_non_zero(pair: Tuple[int, int]) -> None:
|
||||
"""Test division property with any non-zero denominator."""
|
||||
numerator, denominator = pair
|
||||
# Property: (q * d) + r = n, where 0 <= abs(r) < abs(d)
|
||||
quotient = numerator // denominator
|
||||
remainder = numerator % denominator
|
||||
assert (quotient * denominator) + remainder == numerator
|
||||
assert abs(remainder) < abs(denominator)
|
||||
```
|
||||
|
||||
## 3. Performance Considerations
|
||||
|
||||
Configure `hypothesis.settings` for appropriate `max_examples` and `deadline`. Use `pytest` fixtures for expensive setup.
|
||||
|
||||
❌ BAD: Slow tests due to high `max_examples` or expensive setup per run.
|
||||
```python
|
||||
from hypothesis import given, strategies as st, settings
|
||||
import time
|
||||
|
||||
@settings(max_examples=10000) # Too many for local dev
|
||||
@given(st.lists(st.integers(), min_size=1000, max_size=1000))
|
||||
def test_expensive_sort(large_list):
|
||||
# Simulates expensive setup inside the test
|
||||
time.sleep(0.01)
|
||||
assert sorted(large_list) == custom_sort(large_list)
|
||||
```
|
||||
|
||||
✅ GOOD: Sensible `settings`, `pytest` fixtures for setup.
|
||||
```python
|
||||
from hypothesis import given, strategies as st, settings, HealthCheck
|
||||
import pytest
|
||||
import time
|
||||
|
||||
# Global settings for CI/local runs. Override locally if needed.
|
||||
# Use a specific profile for CI, e.g., @settings(my_ci_profile)
|
||||
@settings(max_examples=200, deadline=500, suppress_health_check=[HealthCheck.filter_too_much])
|
||||
@given(st.lists(st.integers(), min_size=1, max_size=100))
|
||||
def test_sort_correctness(data: list[int]) -> None:
|
||||
"""Verify sorting algorithm maintains elements and order."""
|
||||
original_set = set(data)
|
||||
sorted_data = sorted(data)
|
||||
assert len(sorted_data) == len(data)
|
||||
assert set(sorted_data) == original_set
|
||||
assert all(sorted_data[i] <= sorted_data[i+1] for i in range(len(sorted_data) - 1))
|
||||
```
|
||||
|
||||
## 4. Common Pitfalls and Gotchas
|
||||
|
||||
Avoid `assume()` for core logic validation. Test invalid inputs explicitly. Replay failing examples immediately.
|
||||
|
||||
❌ BAD: Using `assume()` to hide bugs or for conditions that should be tested.
|
||||
```python
|
||||
import math
|
||||
from hypothesis import given, strategies as st
|
||||
|
||||
@given(st.integers(min_value=-10, max_value=10))
|
||||
def test_sqrt_positive(n):
|
||||
assume(n >= 0) # Hides behavior for negative numbers, might be a bug
|
||||
assert math.isclose(math.sqrt(n)**2, n)
|
||||
```
|
||||
|
||||
✅ GOOD: Test invalid inputs explicitly or constrain strategies.
|
||||
```python
|
||||
import math
|
||||
from hypothesis import given, strategies as st
|
||||
import pytest
|
||||
|
||||
@given(st.floats(min_value=0.0, allow_nan=False, allow_infinity=False))
|
||||
def test_sqrt_positive_numbers(x: float) -> None:
|
||||
"""Verify sqrt property for non-negative floats."""
|
||||
assert math.isclose(math.sqrt(x)**2, x)
|
||||
|
||||
@given(st.floats(max_value=0.0, exclude_max=True, allow_nan=False, allow_infinity=False))
|
||||
def test_sqrt_negative_numbers_raises_value_error(x: float) -> None:
|
||||
"""Verify sqrt raises ValueError for negative floats."""
|
||||
with pytest.raises(ValueError):
|
||||
math.sqrt(x)
|
||||
```
|
||||
|
||||
## 5. Test Organization and Replaying Failures
|
||||
|
||||
Run tests with `pytest` and use `--hypothesis-show-statistics` for insights. Hypothesis automatically saves and replays failing examples from the `.hypothesis/examples` database.
|
||||
|
||||
```bash
|
||||
# Run all tests
|
||||
pytest
|
||||
|
||||
# Run with Hypothesis statistics
|
||||
pytest --hypothesis-show-statistics
|
||||
|
||||
# Replay a specific failing example (Hypothesis saves them)
|
||||
# If a test fails, pytest will automatically replay the minimal falsifying example
|
||||
# from the .hypothesis/examples directory on subsequent runs.
|
||||
```
|
||||
|
||||
## 6. Mocking Strategies
|
||||
|
||||
Use `pytest-mock` (the `mocker` fixture) for mocking external dependencies. Ensure mocks are scoped correctly (function scope is usually best).
|
||||
|
||||
❌ BAD: Manual mocking or global mocks that persist across tests.
|
||||
```python
|
||||
import os
|
||||
from hypothesis import given, strategies as st
|
||||
|
||||
@given(st.text())
|
||||
def test_read_from_env(key):
|
||||
# This modifies the actual environment!
|
||||
os.environ['TEST_KEY'] = key
|
||||
assert get_env_var('TEST_KEY') == key
|
||||
del os.environ['TEST_KEY'] # Cleanup is error-prone
|
||||
```
|
||||
|
||||
✅ GOOD: Use `mocker` fixture for safe, isolated mocks.
|
||||
```python
|
||||
import os
|
||||
from hypothesis import given, strategies as st
|
||||
import pytest
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
def get_env_var(key: str) -> str | None:
|
||||
return os.getenv(key)
|
||||
|
||||
@given(st.text(min_size=1), st.text())
|
||||
def test_get_env_var_with_mocker(mocker: MockerFixture, key: str, value: str) -> None:
|
||||
"""Verify environment variable retrieval using mocker."""
|
||||
mocker.patch.dict(os.environ, {key: value})
|
||||
assert get_env_var(key) == value
|
||||
|
||||
@given(st.text(min_size=1))
|
||||
def test_get_env_var_missing(mocker: MockerFixture, key: str) -> None:
|
||||
"""Verify missing environment variable returns None."""
|
||||
mocker.patch.dict(os.environ, {}, clear=True) # Ensure env is clean
|
||||
assert get_env_var(key) is None
|
||||
```
|
||||
|
||||
## 7. Type Hints
|
||||
|
||||
Always type hint your test function arguments and custom strategies. This improves readability, maintainability, and enables static analysis.
|
||||
|
||||
❌ BAD: Untyped arguments, unclear strategy return types.
|
||||
```python
|
||||
from hypothesis import given, strategies as st
|
||||
|
||||
@given(st.integers(), st.text())
|
||||
def test_process(num, text):
|
||||
# What are num and text?
|
||||
assert isinstance(num, int)
|
||||
assert isinstance(text, str)
|
||||
```
|
||||
|
||||
✅ GOOD: Explicit type hints for clarity and static analysis.
|
||||
```python
|
||||
from hypothesis import given, strategies as st
|
||||
from typing import Any, TypeVar, Tuple
|
||||
|
||||
T = TypeVar('T')
|
||||
|
||||
@st.composite
|
||||
def custom_data_strategy(draw) -> Tuple[int, str]:
|
||||
"""Generates a tuple of an integer and a string."""
|
||||
num = draw(st.integers(min_value=0, max_value=100))
|
||||
text = draw(st.text(min_size=1, max_size=10))
|
||||
return num, text
|
||||
|
||||
@given(custom_data_strategy())
|
||||
def test_process_data(data: Tuple[int, str]) -> None:
|
||||
"""Process custom generated data."""
|
||||
num, text = data
|
||||
assert isinstance(num, int)
|
||||
assert isinstance(text, str)
|
||||
assert 0 <= num <= 100
|
||||
assert 1 <= len(text) <= 10
|
||||
```
|
||||
Reference in New Issue
Block a user