0
0
Djangoframework~10 mins

Testing API endpoints in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Testing API endpoints
Write test case class
Setup test client and data
Send HTTP request to API endpoint
Receive response
Check response status and data
Pass or fail test
This flow shows how a test case sends a request to an API endpoint and checks the response to confirm correct behavior.
Execution Sample
Django
from django.test import TestCase, Client

class MyApiTest(TestCase):
    def test_get_items(self):
        client = Client()
        response = client.get('/api/items/')
        self.assertEqual(response.status_code, 200)
This code tests a GET request to '/api/items/' and checks if the response status code is 200 (OK).
Execution Table
StepActionInput/RequestResponse StatusResponse DataTest Assertion
1Create test clientNoneN/AN/AN/A
2Send GET requestGET /api/items/200[{"id":1, "name":"Item1"}, {"id":2, "name":"Item2"}]Check status_code == 200
3Assert response status200200N/APass if status_code is 200
4Assert response dataN/A200[{"id":1, "name":"Item1"}, {"id":2, "name":"Item2"}]Pass if data matches expected
5Test endsN/AN/AN/ATest passes if all assertions succeed
💡 Test ends after all assertions pass or fail
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
clientNoneClient instanceClient instanceClient instanceClient instance
responseNoneNoneHttpResponse with status 200 and dataHttpResponse with status 200 and dataHttpResponse with status 200 and data
Key Moments - 3 Insights
Why do we create a test client before sending requests?
The test client simulates a browser or API consumer. It allows sending HTTP requests in tests without running a real server. See Step 1 in execution_table.
What does checking response.status_code == 200 mean?
It means the API responded successfully. Status code 200 means OK. This is shown in Step 3 where the test asserts the response status.
How do we verify the API returns the correct data?
By comparing response data to expected data in Step 4. If they match, the test passes, confirming the API returns correct content.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the response status after sending the GET request?
A404
B200
C500
D302
💡 Hint
Check Step 2 in the execution_table under Response Status
At which step does the test check if the response status code is 200?
AStep 3
BStep 4
CStep 1
DStep 5
💡 Hint
Look at the Test Assertion column in execution_table
If the API returned status 404 instead of 200, what would happen in the test?
ATest would pass
BTest would fail at Step 4
CTest would fail at Step 3
DTest would continue without error
💡 Hint
Refer to Step 3 where status_code is asserted to be 200
Concept Snapshot
Testing API endpoints in Django:
- Use django.test.Client to simulate requests
- Send HTTP methods (GET, POST, etc.) to API URLs
- Check response.status_code for success (200)
- Verify response data matches expected
- Assertions confirm test pass or fail
Full Transcript
Testing API endpoints in Django involves writing test case classes that use the test client to send HTTP requests to API URLs. The client simulates a user or app calling the API. After sending a request, the test receives a response object. The test checks the response status code to ensure the API responded successfully, usually expecting 200 for OK. It also verifies the response data matches what is expected. If all assertions pass, the test passes. This process helps confirm the API works correctly without needing a live server.