【发布时间】:2017-08-25 01:44:22
【问题描述】:
我想构建一种自动化方式来测试我的 Python/Bottle Web 应用程序中的所有路由,因为我目前有大约 100 条路由。做这个的最好方式是什么?
【问题讨论】:
标签: python testing routes bottle
我想构建一种自动化方式来测试我的 Python/Bottle Web 应用程序中的所有路由,因为我目前有大约 100 条路由。做这个的最好方式是什么?
【问题讨论】:
标签: python testing routes bottle
我推荐WebTest;它功能齐全,非常易于使用。这是一个完整的工作示例,演示了一个简单的测试:
from bottle import Bottle, response
from webtest import TestApp
# the real webapp
app = Bottle()
@app.route('/rest/<name>')
def root(name):
'''Simple example to demonstrate how to test Bottle routes'''
response.content_type = 'text/plain'
return ['you requested "{}"'.format(name)]
def test_root():
'''Test GET /'''
# wrap the real app in a TestApp object
test_app = TestApp(app)
# simulate a call (HTTP GET)
resp = test_app.get('/rest/roger')
# validate the response
assert resp.body == 'you requested "roger"'
assert resp.content_type == 'text/plain'
# run the test
test_root()
【讨论】:
unittest + bottle 而不运行 WSGI 服务器。我不知道如何将路线的动态部分放入视图中。
Bottle 的创建者 recommends 使用 WebTest,这是一个专门为单元测试 Python WSGI 应用程序而设计的框架。
另外,还有Boddle,一个专门针对Bottle的测试工具。我自己没有使用过这个软件,所以我不能说它的效果如何,但是,截至发布这个答案时,它似乎得到了积极的维护。
我建议查看其中一个或两个,然后尝试一下。如果您发现自己对如何正确集成其中一个问题还有其他疑问,请发布另一个问题。
祝你好运!
【讨论】: