how to get request object in django unit testing?

前端 未结 5 766
悲&欢浪女
悲&欢浪女 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:23

    See this solution:

    from django.utils import unittest
    from django.test.client import RequestFactory
    
    class SimpleTest(unittest.TestCase):
        def setUp(self):
            # Every test needs access to the request factory.
            self.factory = RequestFactory()
    
        def test_details(self):
            # Create an instance of a GET request.
            request = self.factory.get('/customer/details')
    
            # Test my_view() as if it were deployed at /customer/details
            response = my_view(request)
            self.assertEqual(response.status_code, 200)
    

提交回复
热议问题