requests icon indicating copy to clipboard operation
requests copied to clipboard

feat: Add comprehensive property-based testing suite using Hypothesis

Open tboy1337 opened this issue 3 months ago • 0 comments

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 mechanisms
    • tests/test_hypothesis_cookies.py - Property-based tests for cookie handling
    • tests/test_hypothesis_models.py - Property-based tests for request/response models
    • tests/test_hypothesis_structures.py - Property-based tests for data structures
    • tests/test_hypothesis_utils.py - Property-based tests for utility functions
  • Updated dependencies:

    • Added hypothesis to requirements-dev.txt
  • Updated configuration:

    • Added .hypothesis/ to .gitignore to exclude Hypothesis's working directory from version control

What 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?

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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

tboy1337 avatar Oct 20 '25 13:10 tboy1337