pip install pytest, pytest-django
[pytest]
DJANGO_SETTINGS_MODULE = your_project.settings
import pytest
from django.urls import reverse
from rest_framework.test import APIClient
from rest_framework import status
from book_author.models import Author
@pytest.fixture
def api_client():
return APIClient()
@pytest.fixture
def create_author(db):
return Author.objects.create(name="Django Testing", email="Test Author", genre="FIC")
@pytest.mark.django_db
def test_get_book(api_client, create_author):
url = '/book_author/author/'
response = api_client.get(url)
assert response.status_code == status.HTTP_200_OK
assert response.data['results'][0]['name'] == create_author.name