Django测试:URL映射到基于类的视图

时间:2018-07-19 05:32:01

标签: django django-testing

我是Django测试的新手,因此尝试基本的测试代码。但是它在第二个测试类中显示了一个错误

Tests.py

from django.test import TestCase,Client
from .views import PostList
from django.urls import resolve


class SmokeTest2(TestCase):
    def test_math(self):
        self.assertEqual(1+1,2)

class HomePageTest(TestCase):
    def test_root_url_resolves_to_home_page_view(self):
        found = resolve('/')
        print({'found':found})
        self.assertEqual(found.func(), PostList)

views.py

class PostList(ListView):
    model = Post
    template_name = 'home.html'

urls.py

urlpatterns = [
    path('',views.PostList.as_view(),name ='list'),
]

我在打印found时显示o / p

{'found': ResolverMatch(func=blog.views.PostList, args=(), kwargs={}, url_name=list, app_names=[], namespaces=[])}

但仍然出现此错误

(blog_env) PS D:\django\blog_env\mysite> python manage.py test
D:\django\blog_env\mysite
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
{'found': ResolverMatch(func=blog.views.PostList, args=(), kwargs={}, url_name=list, app_names=[], namespaces=[])}
E.
======================================================================
ERROR: test_root_url_resolves_to_home_page_view (blog.tests.HomePageTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\django\blog_env\mysite\blog\tests.py", line 19, in test_root_url_resolves_to_home_page_view
    self.assertEqual(found.func(), PostList)
TypeError: view() missing 1 required positional argument: 'request'

----------------------------------------------------------------------
Ran 2 tests in 0.069s

FAILED (errors=1)
Destroying test database for alias 'default'... 

5 个答案:

答案 0 :(得分:0)

我认为您错过了使用self.client.get()。尝试以下

class HomePageTest(TestCase):
    def test_root_url_resolves_to_home_page_view(self):
        response = self.client.get(resolve('/'))
        self.assertEqual(response.status_code, 200)


进一步了解Django Test Client

Update-1
我认为about代码可能不起作用,因为.get()方法期望使用path,而我们给出的是view。因此,如有需要,请尝试以下

from django.core.urlresolvers import resolve, reverse


class HomePageTest(TestCase):
    def test_root_url_resolves_to_home_page_view(self):
        response = self.client.get(resolve('/'))
        response = self.client.get(reverse('your_app_name:list'))
        self.assertEqual(response.status_code, 200)

答案 1 :(得分:0)

由于您正在测试基于类的视图,因此从Traceback中可以看出它缺少请求对象。您可以使用django.test软件包提供的RequestFactory。最好阅读以下RequestFactory Documentation以获得更好的了解。它将解决您的问题。

答案 2 :(得分:0)

我刚刚被这个问题困扰,最终在documentation

中找到了解决方案

基于类的视图需要按名称进行比较,因为由于对象ID不同,as_view()生成的函数将不相等,因此该断言应如下所示:

from django.test import TestCase
from django.urls import resolve
from .views import HomePageView

class HomePageViewViewTest(TestCase):
    def test_resolve_to_home_page_view(self):
        resolver = resolve('/')
        self.assertEqual(resolver.func.__name__, HomePageView.as_view().__name__)

答案 3 :(得分:0)

from django.urls import resolve, reverse
class HomePageViewViewTest(TestCase):
def test_resolve_to_home_page_view(self):
    resolver = resolve('/')
    self.assertEqual(resolver.func.view_class, HomePageView)

您可以尝试一下,它对我有用!

答案 4 :(得分:-1)

from django.urls import resolve, reverse


class HomePageTest(TestCase):
    def test_root_url_resolves_to_home_page_view(self):
        response = self.client.get(resolve('/'))
        response = self.client.get(reverse('your_app_name:list'))
        self.assertEqual(response.status_code, 200)