/schemathesis

A tool that generates and runs test cases for Open API / Swagger based apps

Primary LanguagePythonMIT LicenseMIT

Schemathesis

Build codecov.io status for master branch Version Python versions Documentation Status Gitter License

Schemathesis is a tool for testing your web applications built with Open API / Swagger specifications.

It reads the application schema and generates test cases which will ensure that your application is compliant with its schema.

The application under test could be written in any language, the only thing you need is a valid API schema in a supported format.

Supported specification versions:

  • Swagger 2.0
  • Open API 3.0.x

More API specifications will be added in the future.

Built with:

Inspired by wonderful swagger-conformance project.

If you are looking for more information, then there is an article about Schemathesis: https://code.kiwi.com/schemathesis-property-based-testing-for-api-schemas-52811fd2b0a4

Installation

To install Schemathesis via pip run the following command:

Gitter: https://gitter.im/kiwicom/schemathesis

Documentation

For the full documentation, please see https://schemathesis.readthedocs.io/en/latest/ (WIP)

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

Usage

There are two basic ways to use Schemathesis:

CLI is pretty simple to use and requires no coding, in-code approach gives more flexibility.

Command Line Interface

The schemathesis command can be used to perform Schemathesis test cases:

image

If your application requires authorization then you can use --auth option for Basic Auth and --header to specify custom headers to be sent with each request.

To filter your tests by endpoint name, HTTP method or Open API tags you could use -E, -M, -T options respectively.

CLI supports passing options to hypothesis.settings. All of them are prefixed with --hypothesis-:

To speed up the testing process Schemathesis provides -w/--workers option for concurrent test execution:

In the example above all tests will be distributed among 8 worker threads.

If you'd like to test your web app (Flask or AioHTTP for example) then there is --app option for you:

You need to specify an importable path to the module where your app instance resides and a variable name after : that points to your app. Note, app factories are not supported. The schema location could be:

  • A full URL;
  • An existing filesystem path;
  • In-app endpoint with schema.

This method is significantly faster for WSGI apps, since it doesn't involve network.

For the full list of options, run:

Docker

Schemathesis CLI also available as a docker image

To run it against localhost server add --network=host parameter:

Pre-run CLI hook

Sometimes you need to execute custom code before the CLI run, for example setup an environment, register custom string format strategies or modify Schemathesis behavior in runtime you can use --pre-run hook:

NOTE. This option should be passed before the run part.

The passed value will be processed as an importable Python path, where you can execute your code. An example - https://github.com/kiwicom/schemathesis#custom-string-strategies

Registering custom checks for CLI

To add a new check for the Schemathesis CLI there is a special function

The registered check should accept a response with requests.Response / schemathesis.utils.WSGIResponse type and case with schemathesis.models.Case type.

After registration, your checks will be available in Schemathesis CLI and you can use them via -c command line option.

In-code

To examine your application with Schemathesis you need to:

  • Setup & run your application, so it is accessible via the network;
  • Write a couple of tests in Python;
  • Run the tests via pytest.

Suppose you have your application running on http://0.0.0.0:8080 and its schema is available at http://0.0.0.0:8080/swagger.json.

A basic test, that will verify that any data, that fit into the schema will not cause any internal server error could look like this:

It consists of four main parts:

  1. Schema preparation; schemathesis package provides multiple ways to initialize the schema - from_path, from_dict, from_uri, from_file and from_wsgi.
  2. Test parametrization; @schema.parametrize() generates separate tests for all endpoint/method combination available in the schema.
  3. A network call to the running application; case.call does it.
  4. Verifying a property you'd like to test; In the example, we verify that any app response will not indicate a server-side error (HTTP codes 5xx).

NOTE. Look for from_wsgi usage below

Run the tests:

Other properties that could be tested:

  • Any call will be processed in <50 ms - you can verify the app performance;
  • Any unauthorized access will end with 401 HTTP response code;

Each test function should have the case fixture, that represents a single test case.

Important Case attributes:

  • method - HTTP method
  • formatted_path - full endpoint path
  • headers - HTTP headers
  • query - query parameters
  • body - request body

You can use them manually in network calls or can convert to a dictionary acceptable by requests.request:

For each test, Schemathesis will generate a bunch of random inputs acceptable by the 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 behavior.

By default, there will be 100 test cases per endpoint/method combination. To limit the number of examples you could use hypothesis.settings decorator on your test functions:

To narrow down the scope of the schemathesis tests it is possible to filter by method or endpoint:

The acceptable values are regexps or list of regexps (matched with re.search).

WSGI applications support

Schemathesis supports making calls to WSGI-compliant applications instead of real network calls, in this case the test execution will go much faster.

Explicit examples

If the schema contains parameters examples, then they will be additionally included in the generated cases.

With this Swagger schema example, there will be a case with body {"name": "Doggo"}. Examples handled with example decorator from Hypothesis, more info about its behavior is here.

If you'd like to test only examples provided in the schema, you could utilize --hypothesis-phases=explicit CLI option:

Or add this decorator to your test if you use Schemathesis in your Python tests:

NOTE. Schemathesis does not support examples in individual properties that are specified inside Schema Object. But examples in Parameter Object, Media Type Object and Schema Object are supported. See below:

Direct strategies access

For convenience you can explore the schemas and strategies manually:

Schema instances implement Mapping protocol.

Changing data generation behavior

If you want to customize how data is generated, then you can use hooks of three types:

  • Global, which are applied to all schemas;
  • Schema-local, which are applied only for specific schema instance;
  • Test function specific, they are applied only for a specific test function;

Each hook accepts a Hypothesis strategy and should return a Hypothesis strategy:

There are 6 places, where hooks can be applied and you need to pass it as the first argument to schemathesis.hooks.register or schema.register_hook:

  • path_parameters
  • headers
  • cookies
  • query
  • body
  • form_data

It might be useful if you want to exclude certain cases that you don't want to test, or modify the generated data, so it will be more meaningful for the application - add existing IDs from the database, custom auth header, etc.

NOTE. Global hooks are applied first.

Lazy loading

If you have a schema that is not available when the tests are collected, for example it is build with tools like apispec and requires an application instance available, then you can parametrize the tests from a pytest fixture.

In this case the test body will be used as a sub-test via pytest-subtests library.

NOTE: the used fixture should return a valid schema that could be created via schemathesis.from_dict or other schemathesis.from_ variations.

Extending schemathesis

If you're looking for a way to extend schemathesis or reuse it in your own application, then runner module might be helpful for you. It can run tests against the given schema URI and will do some simple checks for you.

runner.prepare creates a generator that yields events of different kinds - BeforeExecution, AfterExecution, etc. They provide a lot of useful information about what happens during tests, but handling of these events is your responsibility. You can take some inspiration from Schemathesis CLI implementation. See full description of events in the source code.

If you want to use Schemathesis CLI with your custom checks, look at this section

The built-in checks list includes the following:

  • Not a server error. Asserts that response's status code is less than 500;
  • Status code conformance. Asserts that response's status code is listed in the schema;
  • Content type conformance. Asserts that response's content type is listed in the schema;
  • Response schema conformance. Asserts that response's content conforms to the declared schema;

You can provide your custom checks to the execute function, the check is a callable that accepts one argument of requests.Response type.

Custom string strategies

Some string fields could use custom format and validators, e.g. card_number and Luhn algorithm validator.

For such cases it is possible to register custom strategies:

  1. Create hypothesis.strategies.SearchStrategy object
  2. Optionally provide predicate function to filter values
  3. Register it via schemathesis.register_string_format

Unittest support

Schemathesis supports Python's built-in unittest framework out of the box, you only need to specify strategies for hypothesis.given:

Schema validation

To avoid obscure and hard to debug errors during test runs Schemathesis validates input schemas for conformance with the relevant spec. If you'd like to disable this behavior use --validate-schema=false in CLI and validate_schema=False argument in loaders.

Local development

First, you need to prepare a virtual environment with poetry. Install poetry (check out the installation guide) and run this command inside the project root:

For simpler local development Schemathesis includes a aiohttp-based server with the following endpoints in Swagger 2.0 schema:

  • /api/success - always returns {"success": true}
  • /api/failure - always returns 500
  • /api/slow - always returns {"slow": true} after 250 ms delay
  • /api/unsatisfiable - parameters for this endpoint are impossible to generate
  • /api/invalid - invalid parameter definition. Uses int instead of integer
  • /api/flaky - returns 1/1 ratio of 200/500 responses
  • /api/multipart - accepts multipart data
  • /api/teapot - returns 418 status code, that is not listed in the schema
  • /api/text - returns plain/text responses, which are not declared in the schema
  • /api/malformed_json - returns malformed JSON with application/json content type header

To start the server:

It is possible to configure available endpoints via --endpoints option. The value is expected to be a comma separated string with endpoint names (success, failure, slow, etc):

Then you could use CLI against this server:

Running tests

You could run tests via tox:

or pytest in your current environment:

Contributing

Any contribution in development, testing or any other area is highly appreciated and useful to the project.

Please, see the CONTRIBUTING.rst file for more details.

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.