如何正确单元测试Django会话? [英] How do I properly unit test a Django session?

查看:199
本文介绍了如何正确单元测试Django会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django会话的行为在标准视图代码和测试代码之间变化,使得测试代码如何为会话编写不清楚。 Google Googling会产生两个有关此问题的相关讨论:



  1. 测试客户端更容易的操纵会话


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


我很困惑,因为这两张票都有不同的处理这个问题的方法,而且都被接受了。我认为这意味着他们被修补,行为现在不同。我也不知道这些修补程序将涉及哪个版本。



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

解决方案

我不太明白你的意思是说行为改变在标准视图和测试代码之间,也许您应该详细说明。



但是关于如何测试会话,我认为有办法。


  1. 您必须了解django会话的工作原理,请阅读您在应用程序中使用的会话包的单元测试。这是关于服务器端的工作原理。

  2. 您可能需要捕获浏览器和服务器之间的几个对话(例如使用FIREBUG)

所以你的问题看起来像是你在与服务器通话时登录服务器时没有传递session_id。喜欢把它放在(POST,GET,COOKIES我不太记得)。



这里重要的是了解HTTP中的会话是如何工作的,一旦你明白了,那么你肯定会清楚地知道发生了什么,并相应地进行解释。 / p>

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?

解决方案

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.

这篇关于如何正确单元测试Django会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆