[Python]Test Driven Development in Flask application


In this recipe, i will describe how to use TDD method to developer Flask application.

from unittest import TestCase, main
from flask import Flask
from flask import request

class MyTest(TestCase):
    
    def test_flask(self):
        app = Flask(__name__)
        app.testing = True
        app.config['SERVER_NAME'] = 'localhost:5000'
        app.config['APPLICATION_ROOT'] = '/demo'
        
        @app.route('/')
        def index():
            return request.url
       
        ctx = app.test_request_context()      
        self.assertEqual(ctx.request.url,'http://localhost:5000/demo/','it is equal')
        with app.test_client()as client :
            rv = client.get('/')
            self.assertEqual(rv.data, 'http://localhost:5000/demo/')

if __name__ == '__main__':
    main()
    


评论关闭