Skip to main content

Hypothesis strategies for Open API / Swagger schemas

Project description

Schemathesis

Schemathesis is a tool that generates test cases for your Open API / Swagger schemas.

The main goal is to bring property-based testing to web applications and verify if all values allowed by the schema are processed correctly by the application.

Empowered by Hypothesis, hypothesis_jsonschema and pytest.

Supported specification versions:

  • Swagger 2.0
  • Open API 3.0.x

NOTE: The library is WIP, the API is a subject to change.

Usage

To generate test cases for your schema you need:

  • Create a parametrizer;
  • Wrap a test with Parametrizer.parametrize method
  • Provide a client and url of a running application instance
import pytest
import requests
from schemathesis import Parametrizer


schema = Parametrizer.from_path("path/to/schema.yaml")

@pytest.fixture(scope="session")
def client():
    with requests.Session() as session:
        yield session

@schema.parametrize()
def test_users_endpoint(client, case):
    url = "http://0.0.0.0:8080" + case.formatted_path
    response = client.request(
        case.method,
        url,
        params=case.query,
        json=case.body
    )
    assert response.status_code == 200

Each wrapped test will have the case fixture, that represents a hypothesis test case.

Case consists of:

  • method
  • formatted_path
  • headers
  • query
  • body

For each schemathesis will create hypothesis strategies which will generate bunch of random inputs acceptable by schema. This data could be used to verify that your application works in the way as described in the schema or that schema describes expected behaviour.

For example the data could be send against running app container via requests and response is checked for an expected status code or error message.

To limit the number of examples you could use hypothesis.settings decorator on your test functions:

from hypothesis import settings

@settings(max_examples=5)
def test_something(client, case):
    ...

Documentation

For full documentation, please see [https://schemathesis.readthedocs.io/en/latest/]

Or you can look at the [docs/] directory in the repository.

Python support

Schemathesis supports Python 3.6, 3.7 and 3.8.

License

The code in this project is licensed under MIT license. By contributing to schemathesis, you agree that your contributions will be licensed under its MIT license.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

schemathesis-0.4.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file schemathesis-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: schemathesis-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3

File hashes

Hashes for schemathesis-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 278684fb857decc112c8f7f0c05581c344fe3293e29ad209551ae31cefb02175
MD5 0a7a5875169bb5b17dde22b7cda87463
BLAKE2b-256 d1429cf1a67e918230ad43e61d9443c3991e34760e3b6432aa45449946dad970

See more details on using hashes here.

Supported by

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