Django 测试客户端响应上下文 None

发布于 2024-10-02 04:56:10 字数 374 浏览 0 评论 0原文

我已将 Django 应用程序从我的开发计算机(OS X、Python 2.6.5、Django 1.2.3)移至临时服务器(Ubuntu VM、Python 2.6.6、Django 1.2.3)。

如果我现在在临时服务器上运行我的测试套件,则使用 Django TestClient 时两个测试会失败,因为 response.context 为 None (但 response.content 是正确的)。

例如:

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

这些测试用例在开发机器上通过。

有人遇到过类似的问题吗?

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?

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

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

发布评论

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

评论(2

梦醒灬来后我 2024-10-09 04:56:10

您需要添加测试设置语句。

import django
django.test.utils.setup_test_environment() 

通过我的链接查找更多详细信息:
http://jazstudios.blogspot.com/2011/01/django-testing -views.html

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

油焖大侠 2024-10-09 04:56:10

来自 Django 文档:

虽然*您的代码*[+]可以在Python交互式解释器中工作,但测试客户端的一些功能,特别是与模板相关的功能,仅在测试运行时可用。原因是 Django 的测试运行程序执行了一些黑魔法,以确定给定视图加载了哪个模板。这种黑魔法(本质上是内存中 Django 模板系统的修补)仅在测试运行期间发生。

因此,如果您在测试运行中运行它,它应该可以工作。

您可以查看这个问题

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文