Testing
Django Ninja is fully compatible with the standard django testing client, but it also provides a testing client so that you can test only the API without the middleware/URL parsing layer, making the tests run faster.
To test the following API:
from ninja import NinjaAPI, Schema
api = NinjaAPI()
router = Router()
class HelloResponse(Schema):
msg: str
@router.get("/hello", response=HelloResponse)
def hello(request):
return {"msg": "Hello World"}
api.add_router("", router)
You can use the Django test class:
from django.test import TestCase
from ninja.testing import TestClient
class HelloTest(TestCase):
def test_hello(self):
client = TestClient(router)
response = client.get("/hello")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {"msg": "Hello World"})
class HelloTest(TestCase):
def test_hello(self):
client = TestClient(router)
# request.company_id will now be set within the view
response = client.get("/hello", company_id=1)
Success
Continue to the next section API documentation
Comments
本文总阅读量次
