[Python]Test Driven Development in Flask application

来源:互联网 发布:淘宝店糯米家和卢洁云 编辑:程序博客网 时间:2024/05/10 19:26

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

from unittest import TestCase, mainfrom flask import Flaskfrom flask import requestclass 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()    


0 0
原创粉丝点击