この動作を簡単にオーバーライドして、同じハンドラーで例外処理を強制することができます。
def run_test(path=None,check_func=None,*args,**kwargs):
with app.test_request_context(path,*args,**kwargs):
try:
data=app.dispatch_request()
if check_func is not None:
check_func()
else:
print data
except Exception as e:
print app.handle_exception(e)
run_test('/')
run_test('/other')
def current_test(data):
assert 'has some content' in data
run_test('/should_be_checked',check_func=current_test)
そしてもう一言。
実際に例外をキャッチするFlaskのその部分を使用していないため、このアプローチは機能しません。コンテキストを直接呼び出しています。
ドキュメントからの引用:
Flask WSGI アプリケーションが内部でどのように機能するかを調べると、次のようなコードが見つかります。
def wsgi_app(self, environ):
with self.request_context(environ):
try:
response = self.full_dispatch_request()
except Exception, e:
response = self.make_response(self.handle_exception(e))
return response(environ, start_response)
しかし!すべてのレベルのすべての Flask メソッドが適切な方法で呼び出されるため、次の方法が正しい方法です。
with app.test_request_context():
with app.test_client() as client:
resp = client.get('/')
#and if you need content of response: print resp.data
---