0
0
Djangoframework~5 mins

Testing API endpoints in Django

Choose your learning style9 modes available
Introduction

Testing API endpoints helps ensure your web service works correctly and returns the right data. It catches errors early and keeps your app reliable.

When you want to check if your API returns the correct data for a request.
When you add new features to your API and want to make sure old parts still work.
When you fix bugs and want to confirm the problem is solved.
When you want to automate checks so you don’t have to test manually every time.
When you want to verify your API handles errors properly.
Syntax
Django
from django.test import TestCase
from rest_framework.test import APIClient

class YourApiTest(TestCase):
    def setUp(self):
        self.client = APIClient()

    def test_endpoint(self):
        response = self.client.get('/api/your-endpoint/')
        self.assertEqual(response.status_code, 200)
        self.assertIn('expected_key', response.json())

Use APIClient from Django REST Framework to simulate API calls.

Use assertEqual and other assertions to check response status and data.

Examples
Test a GET request that should return a list of items.
Django
def test_get_list(self):
    response = self.client.get('/api/items/')
    self.assertEqual(response.status_code, 200)
    self.assertIsInstance(response.json(), list)
Test a POST request to create a new item and check the response data.
Django
def test_post_create(self):
    data = {'name': 'Test Item', 'price': 10}
    response = self.client.post('/api/items/', data, format='json')
    self.assertEqual(response.status_code, 201)
    self.assertEqual(response.json()['name'], 'Test Item')
Test that requesting a non-existing item returns a 404 error.
Django
def test_404_not_found(self):
    response = self.client.get('/api/items/9999/')
    self.assertEqual(response.status_code, 404)
Sample Program

This test class checks three things: getting a list of items, creating a new item, and handling a request for an item that does not exist.

Django
from django.test import TestCase
from rest_framework.test import APIClient

class ItemApiTest(TestCase):
    def setUp(self):
        self.client = APIClient()

    def test_get_items_list(self):
        response = self.client.get('/api/items/')
        self.assertEqual(response.status_code, 200)
        self.assertIsInstance(response.json(), list)

    def test_create_item(self):
        data = {'name': 'Sample Item', 'price': 25}
        response = self.client.post('/api/items/', data, format='json')
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.json()['name'], 'Sample Item')

    def test_get_nonexistent_item(self):
        response = self.client.get('/api/items/9999/')
        self.assertEqual(response.status_code, 404)
OutputSuccess
Important Notes

Always use the setUp method to prepare your test client or data.

Use meaningful test method names starting with test_ so Django can find them.

Run tests often to catch problems early and keep your API working well.

Summary

Testing API endpoints helps keep your web service reliable and bug-free.

Use Django REST Framework's APIClient to simulate requests and check responses.

Write clear tests for different HTTP methods and expected results.