Wednesday, October 31, 2018

Django unit test - response context is None

Hi,

We are using django 1.11 for our ecommerce site.

We are developing unit tests for our views using django testing framework. We are facing issue with a simple implementation:

import unittest


from django.urls import reverse

from django.test import Client


class ViewTests(unittest.TestCase):

    def setUp(self):

        self.client = Client()


    def test_View(self):

        url = '/some-url/'

        

        response = self.client.get(url)

        products = response.context['products']


In the above test case, response.context is coming out to be None and hence, response.context['products'] is throwing exception. Whereas when I run the same on django admin shell, response.context has context object dict. 


I am quite confused why this is happening. I couldn't find any reason in the django documents.


Would really appreciate if someone could help me with the above.  


Thanks,

        

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8e487599-2179-4de6-9a4a-32e108778991%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment