Django test client tests views. FastAPI TestClient tests endpoints.
1# Django test client2from django.test import TestCase, Client34class UserViewTest(TestCase):5 def setUp(self):6 self.client = Client()7 self.user = User.objects.create(8 name="Alice",9 email="alice@test.com"10 )1112 def test_get_users(self):13 response = self.client.get("/api/users/")14 self.assertEqual(response.status_code, 200)15 self.assertEqual(len(response.json()), 1)1617 def test_create_user(self):18 response = self.client.post(19 "/api/users/",20 data=json.dumps({"name": "Bob"}),21 content_type="application/json"22 )23 self.assertEqual(response.status_code, 201)2425# FastAPI test client26from fastapi.testclient import TestClient2728client = TestClient(app)2930def test_read_main():31 response = client.get("/")32 assert response.status_code == 20033 assert response.json() == {"Hello": "World"}3435def test_create_item():36 response = client.post(37 "/items/",38 json={"name": "Foo", "price": 42.0}39 )40 assert response.status_code == 20041 assert response.json()["name"] == "Foo"4243# Async test client44from httpx import AsyncClient4546@pytest.mark.asyncio47async def test_read_main():48 async with AsyncClient(app=app, base_url="http://test") as ac:49 response = await ac.get("/")50 assert response.status_code == 200
Both support: