晚上回家测试了很多方式,都不行。
网上搜索找不到答案,
最后还是官方文档最抵用呢。
https://docs.djangoproject.com/en/2.1/topics/testing/tools/
# my_view here is a function based viewself.assertEqual(response.resolver_match.func, my_view) # class-based views need to be compared by name, as the functions # generated by as_view() won't be equal self.assertEqual(response.resolver_match.func.__name__, MyView.as_view().__name__)
from django.test import TestCasefrom django.urls import resolve, reversefrom .urls import ServerInputListView, ServerInputCreateViewclass UrlTests(TestCase): def test_server_list_view_status_code(self): url = reverse('serverinput:list') response = self.client.get(url) self.assertEqual(response.status_code, 302) def test_server_create_view_status_code(self): url = reverse('serverinput:create') response = self.client.get(url) self.assertEqual(response.status_code, 302) def test_server_list_url_resolves_get_classes_view(self): view = resolve('/server/list/') self.assertEqual(view.func.__name__, ServerInputListView.as_view().__name__) def test_server_create_url_resolves_get_classes_view(self): view = resolve('/server/create/') self.assertEqual(view.func.__name__, ServerInputCreateView.as_view().__name__)