Automated testing using Client - how to test/assert the value of session objects

13 views
Skip to first unread message

Manoj Govindan

unread,
Feb 9, 2007, 1:47:20 AM2/9/07
to Django users
Hi,
I have written a small test making use of test.Client to simulate a
'POST' request to a view. The target view is expected to set a certain
variable in the session on successful handling of the request. Can
anyone suggest a suitable mechanism to check in the test if the
session variable has been set correctly?

Thanks,
Manoj

Margaret

unread,
Feb 9, 2007, 1:49:55 AM2/9/07
to django...@googlegroups.com
would you like paste your code of "test"?


--
mawe...@gmail.com
13585201588

Manoj Govindan

unread,
Feb 9, 2007, 2:03:52 AM2/9/07
to Django users
Here you go.

import unittest
from django.test.client import Client

class BeatTheDrumTests( unittest.TestCase ):
def setUp( self ):
self.client = Client()

def test_POST_btd_select_army( self ):
response = self.client.post("/btd/select_army/",
{"army" : 1})

self.failUnlessEqual(response.status_code, 200, "POST /
btd/select_army/ did not return 200 OK")

# Test for session here. In Rails this would be
something like
# assert_equal( session[:army], 1)


Regards,
Manoj


On Feb 9, 11:49 am, Margaret <mawei...@gmail.com> wrote:
> would you like paste your code of "test"?
>

> On 2/9/07, Manoj Govindan <egma...@gmail.com> wrote:
>
>
>
> > Hi,
> > I have written a small test making use of test.Client to simulate a
> > 'POST' request to a view. The target view is expected to set a certain
> > variable in the session on successful handling of the request. Can
> > anyone suggest a suitable mechanism to check in the test if the
> > session variable has been set correctly?
>
> > Thanks,
> > Manoj
>
> --

> mawei...@gmail.com
> 13585201588

Russell Keith-Magee

unread,
Feb 9, 2007, 6:02:19 AM2/9/07
to django...@googlegroups.com
On 2/9/07, Manoj Govindan <egm...@gmail.com> wrote:
>

Sessions are saved in the database, so you could go straight to the
source, and pull the data from the database. However, you can cheat a
little bit and use the same infrastructure that Django uses in the
Sessions middleware:

from django.contrib.sessions.middleware import SessionWrapper

def MyTest(TestCase):
def testStuff(self):

... do stuff ...
session =
SessionWrapper(self.cookies.get(settings.SESSION_COOKIE_NAME, None))
self.assertTrue(session['key'], value)

Fair warning - I haven't tested this myself. It _should_ work. :-)

Your question does raise an interesting suggestion - the client
currently persists cookies, but it might be nice for the client to
have easy access to the session, too... The testing framework is still
under development, and we need the suggestions of people in the field
to tell us what we can do to make the test framework better.

Yours,
Russ Magee %-)

Manoj Govindan

unread,
Feb 9, 2007, 7:55:40 AM2/9/07
to Django users
Hi Russ,
Thanks for your prompt reply.

I tested the solution you suggested and came up with some questions.
Here they are.

> session =
> SessionWrapper(self.cookies.get(settings.SESSION_COOKIE_NAME, None))
> self.assertTrue(session['key'], value)
>

First of all, I could not find a SESSION_COOKIE_NAME in settings. I
have a feeling that even though I am using a pretty recent SVN
checkout of django, I might not have the correct settings file.

Second, it would seem that the code fragment given above expects the
test class to maintain a list of cookies. Pardon my ignorance, but
should I explicitly create and maintain a list of cookies in the test
class and use them as necessary?
I am confused here, for as the test uses a test client, I thought the
cookie should probably come from the _client_.

Say something like this.

session = SessionWrapper( self.client.cookie )

Client does have a cookie attribute which is initialised to use a
django.http.SimpleCookie object.
Needless to say the above usage failed with a message which I thought
was aimed at me ;-)
" ProgrammingError: can't adapt "

Could you tell me what I am missing here/doing wrong?

Regards,
Manoj

Manoj Govindan

unread,
Feb 9, 2007, 8:17:02 AM2/9/07
to Django users
Addendum:
I tried the following and it did return a session.

session = SessionWrapper( self.client.cookie.get( 'id' ) )

However this session does not have _any_ keys. I am presently checking
my code ...

Manoj Govindan

unread,
Feb 9, 2007, 8:34:43 AM2/9/07
to Django users
Apologies for the flurry of messages.
I just wanted to note that this worked:

session =
SessionWrapper( self.client.cookie.get( 'sessionid' ).value )

The above fragment returned me the session from the db that I was
looking for.
I am still confused. Russell, can I persuade you to explain what is
going on here? :)

Russell Keith-Magee

unread,
Feb 9, 2007, 8:48:08 AM2/9/07
to django...@googlegroups.com
On 2/9/07, Manoj Govindan <egm...@gmail.com> wrote:
>
> Hi Russ,
> Thanks for your prompt reply.
>
> I tested the solution you suggested and came up with some questions.
> Here they are.
>
> > session =
> > SessionWrapper(self.cookies.get(settings.SESSION_COOKIE_NAME, None))
> > self.assertTrue(session['key'], value)
> >
>
> First of all, I could not find a SESSION_COOKIE_NAME in settings. I
> have a feeling that even though I am using a pretty recent SVN
> checkout of django, I might not have the correct settings file.

settings definitely has a SESSION_COOKIE_NAME; it may not be in your
settings file, but it will be inherited from the global default
settings. If you put

from django.conf import settings

at the top of your source file, you should have acess to
SESSION_COOKIE_NAME whether it is in your settings.py or not.

> Second, it would seem that the code fragment given above expects the
> test class to maintain a list of cookies. Pardon my ignorance, but
> should I explicitly create and maintain a list of cookies in the test
> class and use them as necessary?

Sorry - my mistake (I said I didn't run the example! :-)

You are completely correct. The client keeps track of the cookies, so
you need to use self.client.cookie.get(...)

That said - in [4464] I have checked in some changes to the test
client that automagically create the session wrapper for you. If you
update SVN, you will be able to do the following in a test case (as
long as you have the sessions middleware installed):

self.assertEqual(self.client.session['key'], value)

Yours,
Russ Magee %-)

Russell Keith-Magee

unread,
Feb 9, 2007, 8:57:49 AM2/9/07
to django...@googlegroups.com
On 2/9/07, Manoj Govindan <egm...@gmail.com> wrote:
>
> Apologies for the flurry of messages.
> I just wanted to note that this worked:
>
> session =
> SessionWrapper( self.client.cookie.get( 'sessionid' ).value )
>
> The above fragment returned me the session from the db that I was
> looking for.
> I am still confused. Russell, can I persuade you to explain what is
> going on here? :)

self.client.cookie (renamed to cookies in [4464]) is a SimpleCookie
object; this object wraps the ability to easily create HTTP headers
out of a cookie. You need to get the value attribute of the cookie to
return just the value of the session cookie, rather than the full
header information. That value, which is the session id stored as a
cookie, is passed to instantiate the SessionWrapper. SessionWrapper is
essentially a dictionary-like wrapper around Session.objects.get().
The session id instantiates the session wrapper, loading and decoding
the session information from the database, and you query the wrapper
for individual session data.

Does this help explain the process?

Yours,
Russ Magee %-)

Manoj Govindan

unread,
Feb 9, 2007, 9:02:17 AM2/9/07
to Django users
<snip>

> the session information from the database, and you query the wrapper
> for individual session data.
>
> Does this help explain the process?
>

It certainly does.
Thanks!

Reply all
Reply to author
Forward
0 new messages