Junior — Middle
Have you conducted integration tests for API in Django?
sobes.tech AI
Answer from AI
Yes, integration tests for APIs in Django are usually written using the built-in test client APIClient from the rest_framework.test library. They allow testing the interaction between different components of the application, such as the correctness of request handling, serializer functionality, and business logic.
A simple example of an integration test for an API in Django REST Framework:
from rest_framework.test import APIClient
from django.test import TestCase
class APITest(TestCase):
def setUp(self):
self.client = APIClient()
def test_create_item(self):
data = {'name': 'Test item', 'description': 'Test description'}
response = self.client.post('/api/items/', data, format='json')
self.assertEqual(response.status_code, 201)
self.assertEqual(response.data['name'], 'Test item')
This test checks that a POST request creates an object and returns the correct response.