相关疑难解决方法(0)

Django:测试客户端的上下文是空的

我无法从ipython 访问对象的context属性HttpResponse.但单元测试访问context.

这是单元测试.测试运行正常:

from django.test import Client, TestCase
from django.core import mail

class ClientTest(TestCase):
    def test_get_view(self):
        data = {'var': u'\xf2'}
        response = self.client.get('/test04/', data)

        # Check some response details
        self.assertContains(response, 'This is a test')
        self.assertEqual(response.context['var'], u'\xf2')
Run Code Online (Sandbox Code Playgroud)

这是我在shell中使用的代码:

In [10]: from django.test import Client

In [11]: c = Client()

In [12]: r = c.get('/test04/', data)

In [13]: r.context

In [14]: type(r.context)
Out[14]: <type 'NoneType'>
Run Code Online (Sandbox Code Playgroud)

response.context在shell中没有,而response.context在单元测试中存在.

为什么HttpResponseshell和单元测试之间的行为不一致?

testing django unit-testing

7
推荐指数
1
解决办法
2450
查看次数

标签 统计

django ×1

testing ×1

unit-testing ×1