title: How to test template context variables with Flask
body: <p>Django's test client returns a test Response object which includes the template context variables that were used to render the template. <a href="https://docs.djangoproject.com/en/dev/topics/testing/#django.test.client.Response.context">https://docs.djangoproject.com/en/dev/topics/testing/#django.test.client.Response.context</a></p>
  
  <p>How can I get access to template context variables while testing in Flask?</p>
  
  <p>Example view:</p>
  
  <pre><code>@pgt.route('/myview')
  def myview():
      context = {
          'var1': 'value 1',
          'var2': 'value 2',
          'var3': 'value 3',
      }
      return render_template('mytemplate.html', **context)
  </code></pre>
  
  <p>Example test:</p>
  
  <pre><code>class MyViewTestCase(unittest.TestCase):
      def setUp(self):
          self.app = create_app()
          self.client = self.app.test_client()
  
      def test_get_success(self):
          response = self.client.get('/pgt/myview')
  
          # I don't want to do this
          self.assertIn('value 1', response.data)
  
          # I want to do something like this
          self.assertEqual(response.template_context['var1'], 'value 1')
  </code></pre>