繁体   English   中英

使用Python Bottle框架自动测试路线

[英]Automated testing of routes using Python Bottle framework

我想建立一种自动化的方法来测试我的Python / Bottle Web应用程序中的所有路由,因为我目前大约有100条路由。 做这个的最好方式是什么?

我推荐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()

Bottle的创建者建议使用WebTest ,这是一个专门用于单元测试Python WSGI应用程序的框架。

此外,还有Boddle ,这是专门用于Bottle的测试工具。 我没有亲自使用过该软件,因此无法说明它的运行情况,但是,截至本答案发布之时,它似乎已得到积极维护。

我建议您查看其中一个或两个,然后尝试一下。 如果您发现有关如何与其中一个正确集成的其他问题,请发布另一个问题。

祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM