Example Analysis of Django REST framework Unit Test
This article mainly introduces the Django REST framework unit test example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Environment
Win10
Python3.7
Django2.2
Project
Quickly started writing a demo with reference to the official website.
test
Test with reference to the official website
It's similar to Django's test.
Create tutorial/tests/tests.py
Import jsonfrom django.test import TestCasefrom rest_framework import statusfrom rest_framework.test import APIClientclass LittleTestCase (TestCase): def setUp (self): self.click = APIClient () def test_users_post (self) # / users/ POST data = {"username": "tom", "email": "tom@example.com"} response = self.client.post ("/ users/", data) self.assertEqual (response.status_code Status.HTTP_201_CREATED) # / users/:id GET address response_content = json.loads (response.content) user_url = response_content ["url"] # / users/:id GET check whether the new users meet expectations response = self.client.get (user_url) response_content = json.loads (response.content) self.assertEqual (response.status_code, 200) self.assertEqual ("tom" Response_content ["username"]) self.assertEqual ("tom@example.com", response_content ["email"])
Perform a test
Python manage.py test
Python manage.py test tests/
Test result
Successful execution of a test
Create a test database before testing, and delete the database after testing
Creating test database for alias' default'...System check identified no issues (0 silenced)..-Ran 1 test in 0.022sOKDestroying test database for alias' default'... Thank you for reading this article carefully. I hope the article "sample Analysis of Django REST framework Unit Test" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!