Django test utility for validating OpenAPI response documentation
Project description
DRF OpenAPI Tester
DRF OpenAPI Tester is a test utility to validate API responses against OpenAPI 2 and 3 schema. It has built-in support for:
- OpenAPI 2/3 yaml or json schema files.
- OpenAPI 2 schemas created with drf-yasg.
- OpenAPI 3 schemas created with drf-spectacular.
Installation
pip install drf-openapi-tester
Usage
First instantiate one or more instances of SchemaTester:
from openapi_tester import SchemaTester
schema_tester = SchemaTester()
If you are using either drf-yasg or drf-spectacular this will be auto-detected, and the schema will be loaded by the SchemaTester automatically. If you are using schema files though, you will need to pass the file path to the tester:
from openapi_tester import SchemaTester
# path should be a string
schema_tester = SchemaTester(schema_file_path="./schemas/publishedSpecs.yaml")
Once you instantiate a tester, you can use it to validate a DRF Response in a test:
from openapi_tester.schema_tester import SchemaTester
# you need to create at least one instance of SchemaTester.
# you can pass kwargs to it
schema_tester = SchemaTester()
def test_response_documentation(client):
response = client.get('api/v1/test/1')
assert response.status_code == 200
schema_tester.validate_response(response=response)
If you are using the Django testing framework, you can create a base APITestCase that incorporates schema validation:
from openapi_tester.schema_tester import SchemaTester
from rest_framework.test import APITestCase
from rest_framework.response import Response
schema_tester = SchemaTester()
class BaseAPITestCase(APITestCase):
""" Base test class for api views including schema validation """
@staticmethod
def assertResponse(response: Response, **kwargs) -> None:
""" helper to run validate_response and pass kwargs to it """
schema_tester.validate_response(response=response, **kwargs)
Then use it in a test file:
from shared.testing import BaseAPITestCase
class MyAPITests(BaseAPITestCase):
def test_some_view(self):
response = self.client.get("...")
self.assertResponse(response)
Options
We currently support the following optional kwargs:
Case tester
The case tester argument takes a callable to validate the case of both your response schemas and responses. If nothing is passed, case validation is skipped.
The library currently has 4 build-in functions that can be used:
is_pascal_case
is_snake_case
is_camel_case
is_kebab-case
for example:
from openapi_tester import SchemaTester, is_camel_case
schema_test_with_case_validation = SchemaTester(case_tester=is_camel_case)
or
from openapi_tester import SchemaTester, is_camel_case
schema_tester = SchemaTester()
def my_test(client):
response = client.get('api/v1/test/1')
assert response.status_code == 200
schema_tester.validate_response(response=response, case_tester=is_camel_case)
You of course pass your own custom validator function.
Ignore case
List of keys to ignore. In some cases you might want to declare a global list of keys exempt from case testing.
for example:
from openapi_tester import SchemaTester, is_camel_case
schema_test_with_case_validation = SchemaTester(case_tester=is_camel_case, ignore_case=["IP"])
Schema Validation
When the SchemaTester loads a schema, it runs it through OpenAPI Spec validator which validates that the schema passes without specification compliance issues. In case of issues the validator will raise an error.
Known Issues
- We are using prance as a schema resolver, and it has some issues with the resolution of (very) complex OpenAPI 2.0 schemas. If you encounter issues, please document them here.
Contributing
Contributions are welcome. Please see the contributing guide
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 Distribution
Built Distribution
File details
Details for the file drf-openapi-tester-1.2.0.tar.gz
.
File metadata
- Download URL: drf-openapi-tester-1.2.0.tar.gz
- Upload date:
- Size: 17.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.4.0-1039-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 082395fb58d235462deb15ec7252d9573bb265bc94cd709cd3e17d267e7e9279 |
|
MD5 | 82adbe85447b786880b6d34c383240c8 |
|
BLAKE2b-256 | a465050b5d9fc09c3f2eb61fa4fb2563f90decc6d1522f6059d679d1c5e2cffc |
File details
Details for the file drf_openapi_tester-1.2.0-py3-none-any.whl
.
File metadata
- Download URL: drf_openapi_tester-1.2.0-py3-none-any.whl
- Upload date:
- Size: 18.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.4.0-1039-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a11f80901356489ec51c3c5f4b3dce6b9f587173725bd6c146bec8ffe7c963a8 |
|
MD5 | 6db23fe2dae6411aa86bf3e597b39dbf |
|
BLAKE2b-256 | fb62c6a8b8d95acd3bf656b3f8e8fd97662f6ef5530de7defb6b4aa6bb9d4ba9 |