Testing
Django Ninja sepenuhnya kompatibel dengan django testing client standar, tetapi juga menyediakan testing client agar Anda dapat menguji hanya API-nya tanpa lapisan middleware/parsing URL, sehingga test berjalan lebih cepat.
Untuk menguji API berikut:
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)
Anda dapat menggunakan test class Django:
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
Lanjutkan ke bagian berikutnya Dokumentasi API
Komentar
Tampilan halaman:
