# Write Property-Based Tests for a Python Library

An agent adds Hypothesis property tests to httpx: 29 pass, and 5 strict xfails pin decoder and URL bugs the run found, with no library code changed.

[Run this on Rush](https://cloud.getrush.ai/run/write-property-tests/python/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads `httpx/_decoders.py`, `httpx/_urlparse.py`, `httpx/_utils.py`, `_content.py` and `_multipart.py`, then writes `tests/test_properties.py`. The properties cover URL round trips, `normalize_path` and `quote` idempotence, QueryParams, Headers, the gzip, deflate, brotli and zstd decoders under arbitrary chunking, `TextDecoder`, `LineDecoder` against `splitlines`, request bodies, `URLPattern` and Cookies.

Several early failures were the agent's own wrong assumptions: QueryParams groups values by key, `LineDecoder` strips line endings, and `MultiDecoder` applies encodings in reverse header order. It fixed those tests and kept five failures it judged to be library bugs as strict xfails, each with a `FINDING` reason.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Write property-based tests with Hypothesis for the core parsing and transformation functions of this library: round trips, idempotence and invariants. Run them with a high example count. Open a pull request; if a property fails, include the shrunk counterexample and do not change the library.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

Tried on encode/httpx at b5addb6: 1 file changed, 451 insertions(+), no library code touched. It ran in a `uv` venv with `hypothesis` added, since `python3 -m venv` was unavailable. At `HYPOTHESIS_MAX_EXAMPLES=5000` the file ended `30 passed, 5 xfailed in 152.54s (0:02:32)`; after two final deletions the default 200-example run ended `29 passed, 5 xfailed in 6.15s`, and `ruff check .` passed. The full suite's one failure, `tests/test_timeouts.py::test_write_timeout[trio]`, predates the change. The run copied the diff to `/workspace/.rush/artifacts/`, but capture keeps no `.diff` file, so it did not come back; the change is in the run's answer. No pull request was opened.

For the reviewer: keep the five findings as strict xfails, make them plain failures, or fix the library. `hypothesis` is not in `requirements.txt`, and the tests import private modules such as `httpx._decoders`.

## Run it on every night at 2:00 a.m. Pacific

An automation with a `schedule` trigger, `cron` `0 2 * * *` and `timezone` `America/Los_Angeles`, runs this request overnight in the Project's repository. Overnight is where the long example budget fits: the 5,000-example pass took about two and a half minutes for this one file, against about six seconds at the default 200.

## Why run it on Rush?

The existing suite passes with all five present: `DeflateDecoder` raises `incorrect header check` when a raw-deflate stream is split after its first byte or follows an empty first chunk, `ZStandardDecoder` raises `cannot use a decompressobj multiple times` on an empty chunk after the frame ends, `URL("http://0/.")` prints as `http://0` while its `.path` is `/`, and `URL.join` drops an empty `#` fragment marker.

## Frequently asked questions

### What were the shrunk counterexamples?

For raw deflate, `data=b''` split as chunks `[1]`. For zstd, the compressed bytes of `b"abc"` followed by `decode(b"")`. For the join, `URL("http://0/").join("http://0/#")` returns `http://0/`, not `http://0/#`.

### Why do the five findings not fail CI?

They are strict xfails: the suite stays green while each bug exists and turns red as soon as one is fixed, so the mark has to come off with the fix.

## More Python jobs

- [Add Bandit Security Checks to a Python Project's CI](https://getrush.ai/run/add-bandit-to-ci/python/)
- [Add Benchmarks That Catch Performance Regressions in a Python Library](https://getrush.ai/run/add-benchmarks-to-ci/python/)
- [Add Test Coverage Reporting to Python CI](https://getrush.ai/run/add-coverage-to-ci/python/)

## The same job on other stacks

- [Write Property-Based Tests for a Rust Crate](https://getrush.ai/run/write-property-tests/rust/)
- [Write Property-Based Tests for a Rust Library](https://getrush.ai/run/write-property-tests/rust-library/)
- [Write Property-Based Tests for a TypeScript Library](https://getrush.ai/run/write-property-tests/typescript-library/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
