feat: Add comprehensive property-based testing suite using Hypothesis #7053
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces a comprehensive property-based testing suite using the Hypothesis framework, adding extensive test coverage for core requests library modules including authentication, cookies, models, structures, and utilities.
Changes
Added Hypothesis test files (
+2,868 lines):tests/test_hypothesis_auth.py- Property-based tests for authentication mechanismstests/test_hypothesis_cookies.py- Property-based tests for cookie handlingtests/test_hypothesis_models.py- Property-based tests for request/response modelstests/test_hypothesis_structures.py- Property-based tests for data structurestests/test_hypothesis_utils.py- Property-based tests for utility functionsUpdated dependencies:
hypothesistorequirements-dev.txtUpdated configuration:
.hypothesis/to.gitignoreto exclude Hypothesis's working directory from version controlWhat is Hypothesis?
Hypothesis is an advanced property-based testing framework for Python that automatically generates test cases to find edge cases and bugs that traditional example-based tests might miss. Instead of manually writing specific test examples, you describe the properties that your code should satisfy, and Hypothesis generates hundreds of test inputs to verify those properties hold true.
Why is Hypothesis useful?
Discovers edge cases automatically: Hypothesis generates diverse test inputs including boundary values, empty collections, special characters, and unusual combinations that developers often overlook when writing manual tests.
Reduces test maintenance: Instead of maintaining dozens of individual test cases, you write a single property-based test that covers a wide range of scenarios, making tests more concise and maintainable.
Provides reproducible failures: When Hypothesis finds a failing test case, it automatically minimizes the input to the simplest example that reproduces the bug and saves it for future test runs.
Increases confidence: By testing against randomly generated inputs, property-based tests provide stronger guarantees about code correctness across a broader input space than fixed example tests.
Catches regressions: Hypothesis remembers previously-found edge cases and includes them in future test runs, ensuring bugs don't reappear.
For the requests library specifically, Hypothesis helps ensure robust handling of various URL formats, header combinations, authentication schemes, cookie values, and data structures that users might provide in real-world scenarios.
Testing
All new Hypothesis tests pass and can be run with:
pytest tests/test_hypothesis_*.py