How do I properly unit test a Django session?

北慕城南 提交于 2020-01-04 02:43:08

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/2550078/how-do-i-properly-unit-test-a-django-session

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