获取attributeError:' testcase'对象没有属性' assertTemplateused'尝试单位测试Views.py使用Unittest框架

发布于 2025-01-21 12:30:36 字数 688 浏览 1 评论 0 原文

这是我的单位测试水果观点的代码。但是获得 attributeError

test_views.py

class TestViews(unittest.TestCase):

    def test_fruit_GET(self):
        client = Client()
        response = client.get(reverse('products:fruit'))
        self.assertEquals(response.status_code, 200)
        self.assertTemplateUsed(response, 'products/fruit.html')

views.py

def fruit(request):

    product = Product.objects.filter(category="Fruit")
    n = Product.objects.filter(category="Fruit").count()
    params = {'product': product, 'n': n}
    return render(request, 'products/fruit.html', params)

Here is my code for unit test fruit view. But getting AttributeError

test_views.py

class TestViews(unittest.TestCase):

    def test_fruit_GET(self):
        client = Client()
        response = client.get(reverse('products:fruit'))
        self.assertEquals(response.status_code, 200)
        self.assertTemplateUsed(response, 'products/fruit.html')

views.py

def fruit(request):

    product = Product.objects.filter(category="Fruit")
    n = Product.objects.filter(category="Fruit").count()
    params = {'product': product, 'n': n}
    return render(request, 'products/fruit.html', params)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独享拥抱 2025-01-28 12:30:36

此方法是Django的TestCase类的一部分,因此您需要使用它:

from django.test import TestCase

class TestViews(TestCase):

    def test_fruit_GET(self):
        client = Client()
        response = client.get(reverse('products:fruit'))
        self.assertEquals(response.status_code, 200)
        self.assertTemplateUsed(response, 'products/fruit.html')

This method is part of django's TestCase class, so you need to use it instead:

from django.test import TestCase

class TestViews(TestCase):

    def test_fruit_GET(self):
        client = Client()
        response = client.get(reverse('products:fruit'))
        self.assertEquals(response.status_code, 200)
        self.assertTemplateUsed(response, 'products/fruit.html')
行雁书 2025-01-28 12:30:36

这不适用于 unittest.testcase 。这由Django的 class [django-doc] ,所以:

from django.test import TestCase

class TestViews(TestCase):

    def test_fruit_GET(self):
        response = self.client.get(reverse('products:fruit'))
        self.assertEquals(response.status_code, 200)
        self.assertTemplateUsed(response, 'products/fruit.html')

您可以自己实现,但是这是非常不便的:

def assertTemplateUsed(self, response, template_name):
    self.assertIn(
        template_name,
        [t.name for t in response.templates if t.name is not None]
    )

This is not available for the unittest.TestCase. This is provided by Django's TestCase class [Django-doc], so:

from django.test import TestCase

class TestViews(TestCase):

    def test_fruit_GET(self):
        response = self.client.get(reverse('products:fruit'))
        self.assertEquals(response.status_code, 200)
        self.assertTemplateUsed(response, 'products/fruit.html')

You can implement this yourself, but it is very inconvenient:

def assertTemplateUsed(self, response, template_name):
    self.assertIn(
        template_name,
        [t.name for t in response.templates if t.name is not None]
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文