Skip to main content

Test utility for asserting that your API outputs actually match your OpenAPI/Swagger specification.

Project description

https://img.shields.io/pypi/v/openapi-tester.svg https://img.shields.io/pypi/pyversions/openapi-tester.svg https://img.shields.io/pypi/djversions/openapi-tester.svg https://codecov.io/gh/sondrelg/openapi-tester/branch/master/graph/badge.svg https://img.shields.io/badge/code%20style-black-000000.svg https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white

This package provides a simple test utility for testing the integrity of your OpenAPI/Swagger documentation.

The test utility has two main functions. First, the documentation is tested by ensuring that it matches the content of actual API responses, and secondly the package ensures that all documentation adheres to the case-type specified as standard, .e.g, camel case.

The package is currently under development.

Installation

Install using pip:

pip install openapi-tester

Configuration

Add package settings to your settings.py:

SWAGGER_TESTER = {
    'SCHEMA': 'dynamic',
    'CASE': 'camel case'
}

Parameters

  • SCHEMA

    The type of schema you are using. Can either be dynamic or static.

    Default: dynamic

  • CASE
    The case standard you wish to enforce for your documentation. Needs to be one of the following:
    • camel case

    • snake case

    • pascal case

    • kebab case

    • None

    Every key in your tested endpoint’s schema will be verified as compliant or non-compliant according to the selected case, unless you specify None to skip this feature.

    Default: camel case

  • PATH

    The path to your OpenAPI specification.

    This is not required if you’re using a dynamic schema.


Examples

Using drf_yasg for dynamic schema generation, your configuration might look like this:

SWAGGER_TESTER = {
    'SCHEMA': 'dynamic',
    'CASE': 'camel case'
}

While using, e.g., DRF for static schema generation, you would need to add the path to your generated schema:

SWAGGER_TESTER = {
    'SCHEMA': 'dynamic',
    'CASE': 'camel case'
    'PATH': './swagger/schema.json'
}

Implementation

The OpenAPI tester is best used for supplementing your existing API tests.

The easiest way to implement it, is by testing your schema after retrieving a valid response from an endpoint.

An example might look like this:

from django.contrib.auth.models import User
from rest_framework.test import APITestCase

from django_swagger_tester import validate_response


class TestMyAPI(APITestCase):

    def setUp(self):
        user, _ = User.objects.update_or_create(username='test_user')
        self.client.force_authenticate(user=user)
        self.path = '/api/v1/cars'

    def test_get_200(self):
        """
        Verifies that a 200 is returned for a valid GET request to the /correct/ endpoint.
        """
        response = self.client.get(self.path + '/correct' /, headers={'Content-Type': 'application/json'})
        expected_response = [
            {'name': 'Saab', 'color': 'Yellow', 'height': 'Medium', 'width': 'Very wide', 'length': '2 meters'},
            {'name': 'Volvo', 'color': 'Red', 'height': 'Medium', 'width': 'Not wide', 'length': '2 meters'},
            {'name': 'Tesla', 'color': 'black', 'height': 'Medium', 'width': 'Wide', 'length': '2 meters'},
        ]

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json(), expected_response)

        # Test Swagger documentation
        validate_response(response, 'GET', self.path + '/correct/')

See the demo projects and tests folder for more examples.

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

django-swagger-tester-0.1.0.tar.gz (21.0 kB view details)

Uploaded Source

Built Distribution

django_swagger_tester-0.1.0-py3-none-any.whl (32.0 kB view details)

Uploaded Python 3

File details

Details for the file django-swagger-tester-0.1.0.tar.gz.

File metadata

  • Download URL: django-swagger-tester-0.1.0.tar.gz
  • Upload date:
  • Size: 21.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for django-swagger-tester-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c75366ece12d6b0162bfa08b57f992ef4a0bd112cf2f669cb3714ae53ef950cf
MD5 fa0d2369b8c09a6c265ce538b3fa5d37
BLAKE2b-256 da16cc347a16e670fab80e177a00b74d13c2a42d22a1413172b20fe5dbb3545e

See more details on using hashes here.

File details

Details for the file django_swagger_tester-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_swagger_tester-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 32.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for django_swagger_tester-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 996c0ad88bb3a68daed65b8dea6f8306af93a4e4b77041d61193d4efe24fde48
MD5 a92daaad6730f78ac9b5a47ab3152a5d
BLAKE2b-256 098d175578b941efe0840d25d263d3acc053a124db1ce4dcee0f3b31f877cea3

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