Django 的测试客户端返回一个测试响应对象,其中包括用于呈现模板的模板上下文变量。https://docs.djangoproject.com/en/dev/topics/testing/#django.test.client.Response.context
在 Flask 中测试时如何访问模板上下文变量?
示例视图:
@pgt.route('/myview')
def myview():
context = {
'var1': 'value 1',
'var2': 'value 2',
'var3': 'value 3',
}
return render_template('mytemplate.html', **context)
示例测试:
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')