Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Django REST framework Unit Test

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report