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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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