[Fixed]-Unittesting REST API Python

1👍

According to my experience, first you build a web service, second starting to write unittest

Web service for examples:

from rest_framework.views import APIView
class view_example(APIView):
    def get(self, request, format=None):
        ...
    def post(self, request, format=None):
        ...
    def put(self, request, format=None):
        ...
    def delete(self, request, format=None):
        ...

To write unittest when you are sure that you register the view in urls.py.

from rest_framework.test import APITestCase

class ViewExampleTests(APITestCase):
    def test_create(self):
        ...
        response = self.client.post(url, data, format='json')
        ...
    def test_update(self):
        ...
        response = self.client.put(url, data, format='json')
        ...
    def test_delete(self):
        ...
        response = self.client.delete(url, data, format='json')
        ...
    def test_read(self):
        ...
        response = self.client.get(url, data, format='json')
        ...

However, It’s substantial completion of the works.

Leave a comment