一套用于测试烧瓶应用的py.测试夹具。
pytest-flask的Python项目详细描述
一套用于测试烧瓶的pytest夹具 扩展和应用。
功能
plugin提供了一些设备来简化应用程序测试:
- client-实例app.test_client,
- client_class-client用于基于类的测试的fixture,
- config-应用程序配置,
- live_server-在后台运行应用程序(对测试有用 使用Selenium和其他无头浏览器,
- request_ctx-请求上下文,
- accept_json,accept_jsonp,accept_any-接受标题 适合用作client中的参数。
要向应用程序传递选项,请使用pytest.mark.options标记:
@pytest.mark.options(debug=False)deftest_app(app):assertnotapp.debug,'Ensure the app not in debug mode'
在测试执行期间,请求上下文已被推送,例如url_for, session和其他上下文绑定对象在没有上下文的情况下可用 经理:
deftest_app(client):assertclient.get(url_for('myview')).status_code==200
response对象有一个json属性来测试返回 json响应:
@api.route('/ping')defping():returnjsonify(ping='pong')deftest_api_ping(client):res=client.get(url_for('api.ping'))assertres.json=={'ping':'pong'}
如果您希望通过selenium或其他无头浏览器进行测试,请使用 live_server设备。可以使用 url_for函数:
fromflaskimporturl_for@pytest.mark.usefixtures('live_server')classTestLiveServer:deftest_server_is_up_and_running(self):res=urllib2.urlopen(url_for('index',_external=True))assertb'OK'inres.read()assertres.code==200
快速启动
要开始使用插件,请在conftest.py:
中定义应用程序fixturefrommyappimportcreate_app@pytest.fixturedefapp():app=create_app()returnapp
安装带有依赖项的扩展并运行您的测试套件:
$ pip install pytest-flask $ py.test
贡献
不要犹豫为任何bug创建一个GitHub issue,或者 建议。