Skip to content
This repository was archived by the owner on Nov 19, 2023. It is now read-only.

Commit 38745f2

Browse files
committed
docs: add code examples of openapi client usage
1 parent 18d555e commit 38745f2

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

README.md

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ def test_response_documentation(client):
6666
If you are using the Django testing framework, you can create a base APITestCase that incorporates schema validation:
6767

6868
```python
69-
from openapi_tester.schema_tester import SchemaTester
70-
from rest_framework.test import APITestCase
7169
from rest_framework.response import Response
70+
from rest_framework.test import APITestCase
71+
72+
from openapi_tester.schema_tester import SchemaTester
7273

7374
schema_tester = SchemaTester()
7475

@@ -200,8 +201,7 @@ If you wish to validate each response against OpenAPI schema when writing
200201
unit tests - `OpenAPIClient` is what you need!
201202

202203
To use `OpenAPIClient` simply pass `SchemaTester` instance that should be used
203-
to validate responses and then use it like regular Django testing client
204-
(TIP: add custom fixture if you are using `pytest` to avoid code boilerplate):
204+
to validate responses and then use it like regular Django testing client:
205205

206206
```python
207207
schema_tester = SchemaTester()
@@ -210,9 +210,46 @@ response = client.get('/api/v1/tests/123/')
210210
```
211211

212212
To force all developers working on the project to use `OpenAPIClient` simply
213-
override the `client` fixture (when using `pytest-django`) or provide custom
214-
test cases implementation (when using standard Django `unitest`-based approach)
215-
and then you will be sure all newly implemented views will be validated against
213+
override the `client` fixture (when using `pytest` with `pytest-django`):
214+
215+
```python
216+
from pytest_django.lazy_django import skip_if_no_django
217+
218+
from openapi_tester.schema_tester import SchemaTester
219+
220+
221+
@pytest.fixture
222+
def schema_tester():
223+
return SchemaTester()
224+
225+
226+
@pytest.fixture
227+
def client(schema_tester):
228+
skip_if_no_django()
229+
230+
from openapi_tester.clients import OpenAPIClient
231+
232+
return OpenAPIClient(schema_tester=schema_tester)
233+
```
234+
235+
If you are using plain Django test framework, we suggest to create custom
236+
test case implementation:
237+
238+
```python
239+
import functools
240+
241+
from django.test.testcases import SimpleTestCase
242+
from openapi_tester.clients import OpenAPIClient
243+
from openapi_tester.schema_tester import SchemaTester
244+
245+
schema_tester = SchemaTester()
246+
247+
248+
class MyTestCase(SimpleTestCase):
249+
client_class = functools.partial(OpenAPIClient, schema_tester=schema_tester)
250+
```
251+
252+
This will ensure you all newly implemented views will be validated against
216253
the OpenAPI schema.
217254

218255
## Known Issues

tests/test_clients.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import functools
12
import json
23

34
import pytest
5+
from django.test.testcases import SimpleTestCase
46
from rest_framework import status
57

68
from openapi_tester.clients import OpenAPIClient
@@ -73,3 +75,20 @@ def test_request_invalid_response(
7375
"""Ensure ``SchemaTester`` raises an exception when response is invalid."""
7476
with pytest.raises(**raises_kwargs): # noqa: PT010
7577
openapi_client.generic(**generic_kwargs)
78+
79+
80+
def test_django_testcase_client_class():
81+
"""Ensure example from README.md about Django test case works fine."""
82+
83+
class DummyTestCase(SimpleTestCase):
84+
"""Django ``TestCase`` with ``OpenAPIClient`` client."""
85+
86+
client_class = functools.partial(
87+
OpenAPIClient,
88+
schema_tester=SchemaTester(),
89+
)
90+
91+
test_case = DummyTestCase()
92+
test_case._pre_setup()
93+
94+
assert isinstance(test_case.client, OpenAPIClient)

0 commit comments

Comments
 (0)