简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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