我有一个观点,我正在尝试使用Client
对象进行测试。我可以查看我注入render_to_response
视图的变量吗?
示例视图:
def myView(request):
if request.method == "POST":
# do the search
return render_to_response('search.html',{'results':results},context_instance=RequestContext(request))
else:
return render_to_response('search.html',context_instance=RequestContext(request))
测试:
c = Client()
response = c.post('/school/search/', {'keyword':'beagles'})
# how do I get to the 'results'
response.context["results"]
...但是 response.context
和response.template
都返回None
答案 0 :(得分:2)
好吧,找到了我自己的答案。当你自己运行测试时,这些东西没有被填充,但如果你用manage.py test
运行它将被填写。如果你想让独立的测试工作,添加这个在脚本的顶部:
from django.test.utils import setup_test_environment
setup_test_environment()
这是我的整个测试环境设置在我的脚本顶部(供参考):
#!/usr/bin/env python
### Start ENV Setup
import os, sys
sys.path.append('/Users/me/Documents/Web/django_projects/myproject')
from django.core.management import setup_environ
import settings
print "Setting environment to:", setup_environ(settings), "\n"
from django.test.utils import setup_test_environment
setup_test_environment()
### Finish ENV Setup
#-------------------
# Start the Fun! >>
from myproject.myapp.models import mymodel