Skip to main content

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

Project description

This package provides a simple test-utility to test the integrity of your OpenAPI/Swagger documentation against actual API responses.

Package is currently under development, and only supports the testing of swagger documentation implemented in Django using drf_yasg. The ambition for release 1.0.0 is to expand the current features to support testing any openapi specification, and to cut the dependence on Django tooling.

Installation

Install using pip:

pip install openapi-tester

Add ‘openapi_tester’ to your INSTALLED_APPS setting in settings.py:

INSTALLED_APPS = [
   ...
   'drf_yasg',
]

Configuration

The app currently requires two parameters.

Path: The path to your OpenAPI specification. Can be an url, or the path to your document.

Case: The case standard you wish to enforce for your documentation. Can be ‘camelcase’, ‘snakecase’, or None.

  • camelcase: Checks that your documentation is camelCased (default).
  • snakecase: Checks that your documentation is snake_cased.
  • None: Doesn’t check the documentation case standard.

Example

OPENAPI_TESTER_SETTINGS = {
    'PATH': '127.0.0.1:8080/swagger/?format=openapi',
    'CASE': 'camelcase'
}

Example

The OpenAPI tester should primarily be used to supplement your existing API tests.

The easiest way to implement it would be in a test where you’re successfully 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 openapi_tester import validate_specification


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_specification(response, 'GET', self.path + '/correct/')

See the demo project and tests folder for more examples.

Download files

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

Files for openapi-tester, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size openapi_tester-0.0.2-py3-none-any.whl (19.5 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size openapi-tester-0.0.2.tar.gz (12.8 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page