Django test utility for validating OpenAPI response documentation
Project description
DRF OpenAPI Tester
A test utility for validating response documentation
DRF OpenAPI Tester is a simple test utility. Its aim is to make it easy for developers to catch and correct documentation errors in their OpenAPI schemas.
Installation
pip install drf-openapi-tester
How does it work?
Testing your schema is as simple as calling validate_response
at the end
of a regular test.
from openapi_tester.case_testers import is_camel_case
from openapi_tester.schema_tester import SchemaTester
schema_tester = SchemaTester(case_tester=is_camel_case)
def test_response_documentation(client):
response = client.get('api/v1/test/1')
assert response.status_code == 200
assert response.json() == expected_response
schema_tester.validate_response(response=response)
Supported OpenAPI Implementations
Whether we're able to test your schema or not will depend on how it's implemented. We currently support the following:
- Testing dynamically rendered OpenAPI schemas with drf-yasg
- Testing dynamically rendered OpenAPI schemas with drf-spectacular
- Testing any implementation which generates a static yaml or json file (e.g., like DRF)
If you're using another method to generate your schema and would like to use this package, feel free to add an issue or create a PR.
Adding a new implementation is as easy as adding the required logic needed to load the OpenAPI schema.
Features
The primary feature of the schema tester is to validate your API responses with respect to your documented responses. If your schema correctly describes a response, nothing happens; if it doesn't, we throw an error.
The second, optional feature, is checking the case of your response keys. Checking that your responses are camel cased is probably the most common standard, but the package supplies case testers for the following formats:
camelCase
snake_case
PascalCase
kebab-case
The schema tester
The schema tester is a class, and can be instantiated once or multiple times, depending on your needs.
from openapi_tester.schema_tester import SchemaTester
from openapi_tester.case_testers import is_camel_case
tester = SchemaTester(
case_tester=is_camel_case,
ignore_case=['IP'],
schema_file_path=file_path
)
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.
Ignore case
List of keys to ignore. In some cases you might want to declare a global list of exempt keys; keys that you know are not properly cased, but you do not intend to correct.
See the response tester description for info about ignoring keys for individal responses.
Schema file path
This is the path to your OpenAPI schema. This is only required if you use the
StaticSchemaLoader loader class, i.e., you're not using drf-yasg
or drf-spectacular
.
The validate response method
To test a response, you call the validate_response
method.
from .conftest import tester
def test_response_documentation(client):
response = client.get('api/v1/test/1')
tester.validate_response(response=response)
If you want to override the instantiated ignore_case
list,
or case_tester
for a single test, you can pass these directly
to the function.
from .conftest import tester
from openapi_tester.case_testers import is_snake_case
def test_response_documentation(client):
...
tester.validate_response(
response=response,
case_tester=is_snake_case,
ignore_case=['DHCP']
)
Supporting the project
Please leave a ✭ if this project helped you 👏 and contributions are always welcome!
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.1.0.tar.gz
.
File metadata
- Download URL: drf-openapi-tester-1.1.0.tar.gz
- Upload date:
- Size: 18.7 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 | e727e4429d1a8aa7843e81b1a4c9d1c459323d457075d3ac96b9cd409faa74e7 |
|
MD5 | 53613ef63e2937883b976b9b0c691942 |
|
BLAKE2b-256 | 41fa1eda453ff1cfcb86b246543791c576e74096e6d6d3f9b8d5378eefbabb7f |
File details
Details for the file drf_openapi_tester-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: drf_openapi_tester-1.1.0-py3-none-any.whl
- Upload date:
- Size: 19.5 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 | 8b53be22e45ff748952d88728f393df5d2dcbdbb8eaed0c3e43e19b35a4e499c |
|
MD5 | f2866daba0d894b2dd00626fb9175a6a |
|
BLAKE2b-256 | 25d2c31c8ac08383e4f002dbd14a485d457caf25939e425e4b160cc3fd3a40a9 |