Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Unitest Checking Value Of Template Variable

Suppose I have {{registered}} variable in template. I wrote a piece of test:

def nice_test():
     response = self.client.post(reverse('app:register'), {'username': 'dupa'}

and there I want to check value of variable registered in response. How to do it ?

like image 645
sweet_sugar Avatar asked Dec 04 '25 12:12

sweet_sugar


1 Answers

The response from the test client has access to the template context used.

def nice_test():
     response = self.client.post(reverse('app:register'), {'username': 'dupa'})
     self.assertEqual(response.context['registered'], '<expected value>')

Here is a reference to the official documentation: https://docs.djangoproject.com/en/1.7/topics/testing/tools/#django.test.Response.context

class Response
...
context
The template Context instance that was used to render the template that produced the response content.

like image 87
Mark Lavin Avatar answered Dec 07 '25 03:12

Mark Lavin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!