how to get request object in django unit testing?

前端 未结 5 756
悲&欢浪女
悲&欢浪女 2020-12-12 22:15

I have a function as

def getEvents(eid, request):
    ......

Now I want to write unit test for the above function separately (without calli

5条回答
  •  伪装坚强ぢ
    2020-12-12 22:37

    If you are using django test client (from django.test.client import Client) you can access request from response object like this:

    from django.test.client import Client
    
    client = Client()
    response = client.get(some_url)
    request = response.wsgi_request
    

    or if you are using django.TestCase(from django.test import TestCase, SimpleTestCase, TransactionTestCase) you can access client instance in any testcase just by typing self.client:

    response = self.client.get(some_url)
    request = response.wsgi_request
    

提交回复
热议问题