Skip to main content

`pytest --openapi` - an opinionated, lightweight black-box contract tester against a live API using its OpenAPI specification as the source of truth

Project description

Tests & Lint PyPI Downloads Monthly Downloads Documentation License Python Made with Love

🧪 OpenAPI Contract Tester

An opinionated, lightweight black-box contract tester against a live API using its OpenAPI specification as the source of truth.

This tool validates OpenAPI quality, generates test cases from schemas, and verifies that real HTTP responses match the contract. This "certifies" that the documentation is complete with descriptions, example, and schema, and that the endpoint behaves as the documentation suggests.

📚 Read the full documentation

Why?

This package tries to simulate the frustrations of API users, as consumers. With the rise of "agents", this type of documentation-code match became even more important, because LLMs really have trouble choosing tools or using them properly when they do not work as intended.

✨ What it does

▶️ Quick Example

Swagger POST endpoint /email

pytest --openapi=http://localhost:8000
Test #10 ✅
POST /email
Requested:
  {
    "body": "Lorem ipsum dolor sit amet",
    "from": "Lorem ipsum dolor sit amet",
    "subject": "Lorem ipsum dolor sit amet",
    "to": "Test!@#$%^&*()_+-=[]{}|;:<>?,./`~"
  }

Expected 201
  {
    "body": "Hi Bob, how are you?",
    "from": "alice@example.com",
    "id": 1,
    "subject": "Hello",
    "to": "bob@example.com"
  }

Actual 201
  {
    "body": "Lorem ipsum dolor sit amet",
    "from": "Lorem ipsum dolor sit amet",
    "id": 10,
    "subject": "Lorem ipsum dolor sit amet",
    "to": "Test!@#$%^&*()_+-=[]{}|;:<>?,./`~"
  }

Generates multiple QA tests.

✔️ Validates OpenAPI request/response definitions ✔️ Enforces schema field descriptions ✔️ Generates test cases from schemas, checks response codes and types in the response ✔️ Tests the exanples ✔️ Tests GET / POST / PUT / DELETE endpoints ✔️ Compares live responses against examples ✔️ Produces a readable test report

▶️ Detailed Example

Install

pip install pytest-openapi

Run

Say that you have a service running at port 8000 on localhost. Then, run:

pytest --openapi=http://localhost:8000

Options

  • --openapi=BASE_URL: Run contract tests against the API at the specified base URL
  • --openapi-no-strict-example-checking: Use lenient validation for example-based tests

Strict vs Lenient Example Checking

By default, pytest-openapi performs strict matching on example-based tests:

  • When your OpenAPI spec includes explicit request/response examples, the actual response must match the example values exactly
  • This ensures examples accurately reflect real API behavior

However, sometimes examples contain placeholder values (like [1, 2, 3]) that don't match actual responses (like []). Use --openapi-no-strict-example-checking for lenient validation:

pytest --openapi=http://localhost:8000 --openapi-no-strict-example-checking

Lenient mode validates:

  • Structure and types match (all expected keys present, correct types)
  • But ignores exact values and array lengths

Note: Schema-generated tests always use schema validation (not affected by this flag).

Server

See here an example server - email-server: tests/test_servers/email_server/server.py

Resulting Tests

tests/test_servers/email_server/email_test_output.txt

Future Plans / TODO

This is a work in progress.

  • A check that the example matches the schema
  • Ask that 400 responses be in the documentation.
  • A check for regexp and email formats.
  • Extra checks from 200 or 201 messages with missing keys to see 400 messages.
  • Option to turn off the description requirement.

In Consideration

  • Use LLM-as-a-judge to assess the error messages and check their spelling.

Contributing

Contributions are welcome!

The only requirement is 🐳 Docker.

Test are containerized, run them using the VS Code task test. If you don't want to use VS Code, the command is docker compose -f ./tests/docker-compose.yaml --project-directory ./tests up --build --abort-on-container-exit --exit-code-from test. Run this before making a PR, please.

There is also a development environment for VS Code, if you need it. On this environment, you can run the task run-mock-server to run one of the mock servers and see the output.

You can add your own mock server, and then add integration tests. Just follow the same pattern as every test to make a call - subprocess.run('pytest', '--openapi=http://your-server:8000.

Please reformat and lint before making a PR. The VS Task is lint, and if you don't want to use VS Code, the command is: docker compose -f ./lint/docker-compose.yaml --project-directory ./lint up --build --abort-on-container-exit --exit-code-from linter. Run this before making a PR, please.

If you add a functionality, please add to the the documentation.

Please submit a pull request or open an issue for any bugs or feature requests.

The moment your PR is merged, you get a dev release. You can then set up the version number to use your changes.

License

MIT License. See LICENSE file for the specific wording.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_openapi-0.1.3.dev202601191603.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_openapi-0.1.3.dev202601191603-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file pytest_openapi-0.1.3.dev202601191603.tar.gz.

File metadata

File hashes

Hashes for pytest_openapi-0.1.3.dev202601191603.tar.gz
Algorithm Hash digest
SHA256 92d5107f6c6752e55b56ef80c6224fa17be9ca09a1dad8b9acdedfef9706dddc
MD5 3e3ec03df4b525795ca0bb4de49ba197
BLAKE2b-256 8d06a54d9ffc2f0f3965c4f28de28c9b6cc4e9d06e67b17cc3d801baaaae7b56

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_openapi-0.1.3.dev202601191603.tar.gz:

Publisher: ci.yaml on sinan-ozel/pytest-openapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_openapi-0.1.3.dev202601191603-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_openapi-0.1.3.dev202601191603-py3-none-any.whl
Algorithm Hash digest
SHA256 f9b202acf2a4cdfd59f9aefbee679176b65fc1ef9b94d731f1f90701a5f1f07b
MD5 e23c73f5b05e26d854449cf39412ce8c
BLAKE2b-256 2eec593a382e7bf130d000d376a2d112133f7c07d089a346bcb4c9bffd02a1d5

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_openapi-0.1.3.dev202601191603-py3-none-any.whl:

Publisher: ci.yaml on sinan-ozel/pytest-openapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page