如何在ListView中测试numQuery?

2024-09-30 16:25:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在Django 1.7中测试我的列表视图:

class PostList(ListView):
    model = MyBlog
    template_name = 'posts.html'
    queryset = 'some_query'
    context_object_name = 'all_posts'
    paginate_by = 25

class BlogTests(TransactionTestCase):
    fixtures = ['blog.yaml']

    def setUp(self):
        self.client = Client()

    def test_posts_page(self):
        response = self.client.get(reverse_lazy('blog'))
        self.assertEqual(response.status_code, 200, 'incorrect http code in posts page')

    def test_posts_page_num_q(self):
        self.assertNumQueries(3, PostList.as_view())

但我有个错误:

TypeError: view() takes at least 1 argument (0 given)

我不知道,怎么修。你知道吗

拜托,救命啊。你知道吗

谢谢。你知道吗


Tags: djangonametestselfclientview列表response
1条回答
网友
1楼 · 发布于 2024-09-30 16:25:25

TestCase.assertNumQueries将预期的查询数作为第一个参数,将要测试的函数作为第二个参数,将要测试的函数的参数作为其余参数。你知道吗

在您的例子中,要测试的函数是一个需要一个参数的视图:HttpRequest。然后需要将HttpRequest作为第三个参数传递给assertNumQueries。您可以使用RequestFactory为测试目的创建HttpRequest。你知道吗

相关问题 更多 >