title |
---|
Flask / Testing |
Flask / Testing
myapp/hello/__init__.py
:
from flask import Blueprint
blueprint = Blueprint('hello', __name__, url_prefix='/hello')
@blueprint.route('/<somebody>')
def hello(somebody='World'):
return 'Hello, %s!' % somebody
tests/conftest.py
:
from myapp import create_app
import pytest
@pytest.fixture
def client():
app = create_app()
return app.test_client()
tests/test_hello.py
:
def test_hello__somebody__hello_somebody(client):
resp = client.get('/hello/Flask')
assert resp.data == 'Hello, Flask!'
- Testing Flask Applications — Flask Documentation (0.12) #ril
- Something that is untested is broken. 背後的問題在於 Untested applications make it HARD TO IMPROVE existing code
- Flask 揭露了 Werkzeug 的 (test)
Client
(app.test_client()
) 並自動處理 request context,在 test code 裡就能把它當 HTTP client 用。 - 從範例看來
Client.get()/post()
有data
、follow_redirects
等 keyword arguments,但文件在哪呢?
- The Request Context — Flask Documentation (0.12) 解釋了 context locals,也就是 request context,以及如何在沒有 request 時 (例如 testing) 營造出 request context #ril
- test_client() - API — Flask Documentation (0.12) 提到要看
flask.testing.FlaskClient
的文件 #ril - class flask.testing.FlaskClient - API — Flask Documentation (0.12) 要看
werkzeug.test.Client
的文件 #ril - Test Utilities — Werkzeug Documentation (0.14) #ril
- Werkzeug 提供一個
Client
object (werkzeug.test.Client
),傳入 WSGI application (及非必要的 response wrapper),就可以對它送 virtual request。
- Werkzeug 提供一個
- class werkzeug.test.Client - Test Utilities — Werkzeug Documentation (0.14)
open()
接受跟EnvironBuilder
一樣的參數 #ril - class werkzeug.test.EnvironBuilder - Test Utilities — Werkzeug Documentation (0.14) 終於看到
query_string
、data
等參數 #ril - python - How can I fake request.POST and GET params for unit testing in Flask? - Stack Overflow #ril
手冊: