Package: drf-test-generator
drf-test-generatorThis is a Django Package that generates basic tests for Django REST Framework.FeaturesGenerates tests for allViewSetsin a Django REST FrameworkrouterGenerates tests for all customactionsin aViewSetGenerates tests for allHTTPmethods in aViewSetSupports test generation for bothpytestandunittestInstallationpip install drf-test-generatorSetupAdddrf_test_generatorto yourINSTALLED_APPSinsettings.py:INSTALLED_APPS=[# ..."drf_test_generator",]Note:This is required for the Django Management Command. You can remove it fromINSTALLED_APPSonce you have generated the tests.Generate TestsDjango Management CommandYou can generate tests using the Django Management Command:python manage.py generate_viewset_tests -r api.urls.routerNote:Currently this package only supports test generation for ViewSets that are registered in a router.Django Management Command Options-ror--router(REQUIRED)The dotted path to the REST framework url router. This is the router that contains the ViewSets you want to generate tests for.Example:--router api.urls.router--test-base-classThe dotted path to the test base class. This is the class that your tests will inherit from. If not provided, the tests will inherit fromrest_framework.test.APITestCase.Example:--test-base-class tests.base.MyCustomTest--namespaceThe namespace to use for the URLs (e.g:api_v1:posts-list). If not provided, no namespace will be used.Example:--namespace api_v1--output-fileThe path to the output file. If not provided, the tests will be printed to the console.Example:--output-file tests.py--select-viewsetsA list of ViewSets to generate tests for. If not provided, tests will be generated for all ViewSets in the router.Example:--select-viewsets PostViewSet CommentViewSet--variantThe test variant to generate. Options:pytest,unittest. If not provided,unittestwill be used.Example:--variant pytest--pytest-markersA list of pytest markers to add to the generated tests. If not provided, only the@pytest.mark.django_dbmarker will be added.Example:--pytest-markers pytest.mark.ignore_template_errors pytest.mark.urls('myapp.test_urls')--pytest-fixturesA list of pytest fixtures to add to the generated tests. If not provided, only theclientfixture will be added.Example:--pytest-fixtures django_user_modelExamples UsageFor the following code# api/views.pyfromrest_frameworkimportviewsetsfromrest_framework.decoratorsimportactionfromrest_framework.responseimportResponseclassPostViewSet(viewsets.ModelViewSet):# ...@action(detail=False,methods=["get","post"],url_path="custom-action")defcustom_action(self,request):returnResponse()@action(detail=True,methods=["get","options","put"],url_path="new-action")defcustom_action2(self,request):returnResponse()# api/urls.pyfromrest_frameworkimportroutersrouter=routers.DefaultRouter()router.register("posts",PostViewSet,basename="post")For UnittestRun Commandpython manage.py generate_viewset_tests -r api.urls.routerOutputfromdjango.urlsimportreversefromrest_frameworkimportstatusfromrest_framework.testimportAPITestCaseclassPostViewSetTests(APITestCase):deftest_post_list_get(self):url=reverse("post-list")response=self.client.get(url)self.assertEqual(response.status_code,status.HTTP_200_OK)deftest_post_create_post(self):url=reverse("post-list")response=self.client.post(url,data={})self.assertEqual(response.status_code,status.HTTP_201_CREATED)deftest_post_custom_action_get(self):url=reverse("post-custom-action")response=self.client.get(url)self.assertEqual(response.status_code,status.HTTP_200_OK)deftest_post_custom_action_post(self):url=reverse("post-custom-action")response=self.client.post(url,data={})self.assertEqual(response.status_code,status.HTTP_201_CREATED)deftest_post_retrieve_get(self):url=reverse("post-detail",kwargs={"pk":None})response=self.client.get(url)self.assertEqual(response.status_code,status.HTTP_200_OK)deftest_post_update_put(self):url=reverse("post-detail",kwargs={"pk":None})response=self.client.put(url,data={})self.assertEqual(response.status_code,status.HTTP_200_OK)deftest_post_partial_update_patch(self):url=reverse("post-detail",kwargs={"pk":None})response=self.client.patch(url,data={})self.assertEqual(response.status_code,status.HTTP_200_OK)deftest_post_destroy_delete(self):url=reverse("post-detail",kwargs={"pk":None})response=self.client.delete(url)self.assertEqual(response.status_code,status.HTTP_204_NO_CONTENT)deftest_post_custom_action2_get(self):url=reverse("post-custom-action2",kwargs={"pk":None})response=self.client.get(url)self.assertEqual(response.status_code,status.HTTP_200_OK)deftest_post_custom_action2_options(self):url=reverse("post-custom-action2",kwargs={"pk":None})response=self.client.options(url)self.assertEqual(response.status_code,status.HTTP_200_OK)deftest_post_custom_action2_put(self):url=reverse("post-custom-action2",kwargs={"pk":None})response=self.client.put(url,data={})self.assertEqual(response.status_code,status.HTTP_200_OK)For PytestRun Commandpython manage.py generate_viewset_tests -r api.urls.router --variant pytestOutputfromdjango.urlsimportreversefromrest_frameworkimportstatusimportpytest# PostViewSet Tests@pytest.mark.django_dbdeftest_post_list_get(client):url=reverse("post-list")response=client.get(url)assertresponse.status_code==status.HTTP_200_OK@pytest.mark.django_dbdeftest_post_create_post(client):url=reverse("post-list")response=client.post(url,data={})assertresponse.status_code==status.HTTP_201_CREATED@pytest.mark.django_dbdeftest_post_custom_action_get(client):url=reverse("post-custom-action")response=client.get(url)assertresponse.status_code==status.HTTP_200_OK@pytest.mark.django_dbdeftest_post_custom_action_post(client):url=reverse("post-custom-action")response=client.post(url,data={})assertresponse.status_code==status.HTTP_201_CREATED@pytest.mark.django_dbdeftest_post_retrieve_get(client):url=reverse("post-detail",kwargs={"pk":None})response=client.get(url)assertresponse.status_code==status.HTTP_200_OK@pytest.mark.django_dbdeftest_post_update_put(client):url=reverse("post-detail",kwargs={"pk":None})response=client.put(url,data={})assertresponse.status_code==status.HTTP_200_OK@pytest.mark.django_dbdeftest_post_partial_update_patch(client):url=reverse("post-detail",kwargs={"pk":None})response=client.patch(url,data={})assertresponse.status_code==status.HTTP_200_OK@pytest.mark.django_dbdeftest_post_destroy_delete(client):url=reverse("post-detail",kwargs={"pk":None})response=client.delete(url)assertresponse.status_code==status.HTTP_204_NO_CONTENT@pytest.mark.django_dbdeftest_post_custom_action2_get(client):url=reverse("post-custom-action2",kwargs={"pk":None})response=client.get(url)assertresponse.status_code==status.HTTP_200_OK@pytest.mark.django_dbdeftest_post_custom_action2_options(client):url=reverse("post-custom-action2",kwargs={"pk":None})response=client.options(url)assertresponse.status_code==status.HTTP_200_OK@pytest.mark.django_dbdeftest_post_custom_action2_put(client):url=reverse("post-custom-action2",kwargs={"pk":None})response=client.put(url,data={})assertresponse.status_code==status.HTTP_200_OKLicenseThe code in this project is released under theMIT License.