vlad-ardelean
vlad-ardelean

Reputation: 7622

(How) can I set session data from inside a django test?

I'm relying on some session data for my django tests. I was wondering if i could still do that or if i need to rethink my logic.

Anyway, i'm trying to set the session data like this:

self.client.session['last_page_id'] = unicode(self.t1_p5.id)

response = self.client.post(final_page_path, final_page_answers,
                                    follow=True) 

So not only is the self.client.session attribute an "empty" after the assignment, but also my session is actually empty at the time it is being used - and yes the value assigned does indeed exist - no AttributeError is raised.

[EDIT] django version 1.6

Upvotes: 0

Views: 158

Answers (1)

Mariy
Mariy

Reputation: 5924

I don't know which version of Django you use but in some old docs it is written:

To modify the session and then save it, it must be stored in a variable first (because a new SessionStore is created every time this property is accessed):

Example:

def test_something(self):
    session = self.client.session
    session['somekey'] = 'test'
    session.save()

docs

Upvotes: 1

Related Questions