Django 1.0 Testing: How do I get the session to persist between the test code and the view under test?

I am trying to check how the view behaves when there is certain data stored in the session. To do this, I created a session in the test method and brought up an interactive shell at the very beginning of the view:

Test Method:

def test_user_with_unused_tests(self):
    "User is given a test and sent to test start"
    # todo: insure that the user is given a test that he hasn't done

    #login
    login = self.client.login(username='xxx', password='xxx')
    self.failUnless(login)

    # build the screener
    user = User(username='xxx', password='xxx')
    user_screener = UserScreener(user=user)

    # put the screener in session
    self.client.session['user_screener'] = user_screener

      

View that tested:

@login_required
def screener_start(request):

    import code
    code.interact(local=locals())

      

But apparently the session is not persisting between my test method and the view call:

Intolerance certificate:

>>> request.session.values()
[1, 'django.contrib.auth.backends.ModelBackend']

      

Is there a way to fix this? Am I missing something?

I am using Django 1.0.

Thanks in advance for your thoughts.

+2


source to share


1 answer


Sounds like you need a method setUp

, please see http://docs.python.org/library/unittest.html for method documentation setUp

and tearDown

. SetUp will be run for all tests, the code for each individual test will be destroyed at the end of each test run.

Essentially, you need to put the login login in setUp

and the actual test logic in the test method.



Hope it helps

0


source







All Articles