How to use session in TestCase in Django?

前端 未结 5 1081
误落风尘
误落风尘 2020-12-10 03:36

I would like to read some session variables from a test (Django TestCase)

How to do that in a clean way ?

def test_add_docs(self):
    \"\"\"
    Tes         


        
相关标签:
5条回答
  • 2020-12-10 04:07

    As of Django 1.7+ this is much easier. Make sure you set the session as a variable instead of referencing it directly.

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

    andreaspelme's workaround is only needed in older versions of django. See docs

    0 讨论(0)
  • 2020-12-10 04:12

    If you need to initialize a session for the request in tests to manipulate it directly:

    from django.contrib.sessions.middleware import SessionMiddleware
    from django.http import HttpRequest
    
    
    request = HttpRequest()
    
    middleware = SessionMiddleware()
    middleware.process_request(request)
    
    request.session.save()
    
    0 讨论(0)
  • 2020-12-10 04:16

    Unfortunately, this is not a easy as you would hope for at the moment. As you might have noticed, just using self.client.session directly will not work if you have not called other views that has set up the sessions with appropriate session cookies for you. The session store/cookie must then be set up manually, or via other views.

    There is an open ticket to make it easier to mock sessions with the test client: https://code.djangoproject.com/ticket/10899

    In addition to the workaround in the ticket, there is a trick that can be used if you are using django.contrib.auth. The test clients login() method sets up a session store/cookie that can be used later in the test.

    If you have any other views that sets sessions, requesting them will do the trick too (you probably have another view that sets sessions, otherwise your view that reads the sessions will be pretty unusable).

    from django.test import TestCase
    from django.contrib.auth.models import User
    
    class YourTest(TestCase):
        def test_add_docs(self):
            # If you already have another user, you might want to use it instead
            User.objects.create_superuser('admin', 'foo@foo.com', 'admin')
    
            # self.client.login sets up self.client.session to be usable
            self.client.login(username='admin', password='admin')
    
            session = self.client.session
            session['documents_to_share_ids'] = [1]
            session.save()
    
            response = self.client.get('/')  # request.session['documents_to_share_ids'] will be available
    
    0 讨论(0)
  • 2020-12-10 04:25

    You should be able to access a Client's session variales through its session property, so I guess self.client.session['documents_to_share_ids'] = [1] should be what you are looking for!

    0 讨论(0)
  • 2020-12-10 04:26

    If testing my django project with pytest, I can not see any modifications to the session that are made in the view. (That is because the Sessions middleware doesn't get called.)

    I found the following approach to be useful:

    from unittest.mock import patch
    from django.test import Client
    from django.contrib.sessions.backends import db
    
    def test_client_with_session():
        client = Client()
        session = {}  # the session object that will persist throughout the test
        with patch.object(db, "SessionStore", return_value=session):
            client.post('/url-that-sets-session-key/')
    
            assert session['some_key_set_by_the_view']
    
            client.get('/url-that-reads-session-key/')
    

    This approach has the benefit of not requiring database access.

    0 讨论(0)
提交回复
热议问题