如何正确地对 Django 会话进行单元测试?

发布于 2024-08-27 00:55:54 字数 493 浏览 2 评论 0原文

Django 会话的行为在“标准”视图代码和测试代码之间发生变化,这使得我们不清楚如何为会话编写测试代码。谷歌搜索得出关于此问题的两个相关讨论:

  1. 通过以下方式更轻松地操纵会话 测试客户端

  2. test.Client.session.save() 引发 匿名用户的错误

我很困惑,因为两张票都有不同的方式处理这个问题,而且它们都被接受了。我认为这意味着它们已被修补并且现在的行为有所不同。我也不知道这些补丁适用于哪些版本。

如果我在 Django 1.0 中编写单元测试,我将如何设置会话存储以使会话像在浏览器中一样工作?

The behavior of Django sessions changes between "standard" views code and test code, making it unclear how test code is written for sessions. Googling this yields two relevant discussions about this issue:

  1. Easier manipulation of sessions by
    test client

  2. test.Client.session.save() raises
    error for anonymous users

I'm confused because both tickets have different ways of dealing with this problem and they were both Accepted. I assume this means they were patched and the behavior is now different. I also don't know to which versions these patches would pertain.

If I'm writing a unit test in Django 1.0, how would I set up my session store for sessions to work as they do in the browser?

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

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

发布评论

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

评论(1

撩动你心 2024-09-03 00:55:54

我不太明白你所说的“标准”视图和“测试”代码之间的行为变化是什么意思,也许你应该详细说明这一点。

但关于如何测试会话,我确实认为有一些方法。

  1. 您必须了解 django session 的工作原理,阅读应用程序中使用的 session 包的单元测试。这是关于了解服务器端如何工作的。
  2. 您可能需要捕获浏览器和服务器之间的一些对话(例如使用 FIREBUG ),

因此您的问题看起来像是您在与服务器对话时没有传递登录回服务器时获得的 session_id 。就像把它放进去(POST、GET、COOKIES 我不太记得了)。

这里重要的是理解会话在 HTTP 中是如何工作的,一旦你明白了,你肯定对那里发生的事情有一个清晰的了解,并做出相应的解释。

I don't quite understand what do you mean by saying the behavior changes between "standard" view and "test" code, maybe you should elaborate on that.

but regarding how to test the session, I do think there are approaches.

  1. you have to understand how django session works, read the unit test for the session package you used in your application. this is regarding understand how server side works.
  2. you probably need to capture a few conversations between browser and server( using FIREBUG for example )

so the issue for you looks like that you are not passing session_id you get when you log in back to server when you talk to server. like put it in (POST,GET,COOKIES I don't quite remember that ).

The important thing here is understand how session works in HTTP, once you get that, you definitely have a clear idea about what is happening there, and make explainations accordingly.

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