简体   繁体   English

如何正确单元测试Django会话?

[英]How do I properly unit test a Django session?

The behavior of Django sessions changes between "standard" views code and test code, making it unclear how test code is written for sessions. Django会话的行为在“标准”视图代码和测试代码之间变化,使得不清楚如何为会话编写测试代码。 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 test.Client.session.save()会引发匿名用户的错误

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? 如果我在Django 1.0中编写单元测试,我如何设置会话存储以使会话像在浏览器中那样工作?

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. 您必须了解django会话的工作原理,阅读您在应用程序中使用的会话包的单元测试。 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 ) 您可能需要在浏览器和服务器之间捕获一些对话(例如使用FIREBUG)

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. 因此,当您与服务器通信时,当您登录服务器时,您的问题看起来就像是没有传递session_id。 like put it in (POST,GET,COOKIES I don't quite remember that ). 喜欢把它放进去(POST,GET,COOKIES我不太记得)。

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. 这里重要的是理解会话如何在HTTP中工作,一旦你得到它,你肯定对那里发生的事情有一个明确的想法,并相应地进行解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM