如何避免通过test_request_context测试Flask应用程序?

时间:2019-12-03 13:18:45

标签: python flask applicationcontext flask-testing

要测试视图,我目前必须传递“测试请求上下文”才能对其进行测试。怎样做才能避免通过?

def test_fun(self):
    with app.test_request_context() and app.app_context():
       # within this block, current_app points to app.
       print(current_app.name)
       with pyodbc.connect(connectionString) as conn:
           cursor = conn.cursor()
           my_view()

#this is the view that I want to test
@app.route("/",methods=['GET','POST']
def my_view()
    with app.test_request_context()#<-- Is this required??? without this I get RuntimeError-working outside of request context
    '''
    does something
    '''
    return render_template('my_template.html')

0 个答案:

没有答案