[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()
    


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。