Writing basic tests in DRF
Let us write a basic test for an API that sends a static response. First, let us have a basic API that returns "hello world!"
as a response to a GET
request. Add the following code to the blog/views.py
file:
from rest_framework import status from rest_framework.decorators import api_view from rest_framework.response import Response @api_view(['GET']) def basic_req(request): if request.method == 'GET': resp = {"msg": "hello world!"} return Response(data=resp, status=status.HTTP_200_OK)
The URL is configured as /blog/hello-world/
, by adding the following code to the blog/urls.py
file:
urlpatterns = [ path("hello-world/", views.basic_req), ]
We have now created a basic URL that responds with a "hello world"
response. We will now write a test case to verify the response for this API. In the following...