@@ -66,9 +66,10 @@ def test_response_documentation(client):
66
66
If you are using the Django testing framework, you can create a base APITestCase that incorporates schema validation:
67
67
68
68
``` python
69
- from openapi_tester.schema_tester import SchemaTester
70
- from rest_framework.test import APITestCase
71
69
from rest_framework.response import Response
70
+ from rest_framework.test import APITestCase
71
+
72
+ from openapi_tester.schema_tester import SchemaTester
72
73
73
74
schema_tester = SchemaTester()
74
75
@@ -200,8 +201,7 @@ If you wish to validate each response against OpenAPI schema when writing
200
201
unit tests - ` OpenAPIClient ` is what you need!
201
202
202
203
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:
205
205
206
206
``` python
207
207
schema_tester = SchemaTester()
@@ -210,9 +210,46 @@ response = client.get('/api/v1/tests/123/')
210
210
```
211
211
212
212
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
216
253
the OpenAPI schema.
217
254
218
255
## Known Issues
0 commit comments