Django test client response context None

我的未来我决定 提交于 2019-12-05 11:34:27

问题


I have moved my Django app from my development machine (OS X, Python 2.6.5, Django 1.2.3) to a staging server (Ubuntu VM, Python 2.6.6, Django 1.2.3).

If I now run my test suite on the staging server, two tests fail when using the Django TestClient because response.context is None (but response.content is correct).

For example:

self.assertEquals(self.session.pk, response.context['db_session'].pk)

These test cases pass on the development machine.

Has anybody encountered similar problems?


回答1:


You need to add the test setup statement.

import django
django.test.utils.setup_test_environment() 

Find more details by following my link: http://jazstudios.blogspot.com/2011/01/django-testing-views.html




回答2:


From Django documentation:

Although * your code * [+] would work in the Python interactive interpreter, some of the test client's functionality, notably the template-related functionality, is only available while tests are running. The reason for this is that Django's test runner performs a bit of black magic in order to determine which template was loaded by a given view. This black magic (essentially a patching of Django's template system in memory) only happens during test running.

So if you run it in a test run, it should work.

You can see this question



来源:https://stackoverflow.com/questions/4143384/django-test-client-response-context-none

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!