Test Flask render_template() context

前端 未结 4 953
滥情空心
滥情空心 2020-12-24 12:02

I have a Flask route that looks like this:

@app.route(\'/\')                                                                 
def home():                             


        
相关标签:
4条回答
  • 2020-12-24 12:22

    You can use the assert_template_used method of TestCase provided by flask-testing.

    from flask.ext.testing import TestCase
    
    class MyTest(TestCase):
    
        def create_app(self):
            return myflaskapp
    
        def test_greeting(self):
            self.app.get('/')
            self.assert_template_used('hello.html')
            self.assert_context("greeting", "hello")
    

    The method create_app must provide your flask app.

    0 讨论(0)
  • 2020-12-24 12:36

    Flask official documentation suggests that you use the template_rendered signal (available since version 0.6) for unit-testing your templates and the variables used to render it.

    For example, here is a helper context manager that can be used in a unittest to determine which templates were rendered and what variables were passed to the template:

    from flask import template_rendered
    from contextlib import contextmanager
    
    @contextmanager
    def captured_templates(app):
        recorded = []
        def record(sender, template, context, **extra):
            recorded.append((template, context))
        template_rendered.connect(record, app)
        try:
            yield recorded
        finally:
            template_rendered.disconnect(record, app)
    

    This can now easily be paired with a test client:

    with captured_templates(app) as templates:
        rv = app.test_client().get('/')
        assert rv.status_code == 200
        assert len(templates) == 1
        template, context = templates[0]
        assert template.name == 'index.html'
        assert len(context['items']) == 10
    
    0 讨论(0)
  • 2020-12-24 12:41

    My suggestion would be to take a look at the Flask documentation for testing.

    Using the docs as a guide you should be able to set up a test case that can check the contents of the response.

    import unittest
    import yourappname
    
    class MyAppTestCase(unittest.TestCase):
        def setUp(self):
            self.app = yourappname.app.test_client()
    
        def test_greeting(self):
            rv = self.app.get('/')
            self.assertIn('hello', rv.data)
    

    where yourappname is the name of your app/project.

    0 讨论(0)
  • 2020-12-24 12:41

    You may want to use Jinja setup in your Html page, pass the variable to the page and see if updated.

    http://flask.pocoo.org/docs/0.11/templating/#jinja-setup

    http://jinja.pocoo.org/

    As an example:

    flask template

    @app.route('/')                                                                 
    def home():                                                                                                                  
        return render_template(                                                     
            'home.html',                                                            
            greetingDictionary = {"greeting": "hello" , "forthoseabouttorock" :"wesaluteyou" }                                       
        )   
    

    html page

    {% for key in greetingDictionary %}
    <h1>{{key}}</h1>
    <p>{{greetingDictionary[key]}}</p>
    {% endfor %}
    
    0 讨论(0)
提交回复
热议问题