【问题标题】:Automated testing of routes using Python Bottle framework使用 Python Bottle 框架自动测试路线
【发布时间】:2017-08-25 01:44:22
【问题描述】:

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

【问题讨论】:

    标签: python testing routes bottle


    【解决方案1】:

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

    【讨论】:

    • 在这种情况下我必须运行 WSGI 服务器吗?目前,我正在使用 unittest + bottle 而不运行 WSGI 服务器。我不知道如何将路线的动态部分放入视图中。
    • 不,您不需要服务器。 WebTest 代替了服务器。
    【解决方案2】:

    Bottle 的创建者 recommends 使用 WebTest,这是一个专门为单元测试 Python WSGI 应用程序而设计的框架。

    另外,还有Boddle,一个专门针对Bottle的测试工具。我自己没有使用过这个软件,所以我不能说它的效果如何,但是,截至发布这个答案时,它似乎得到了积极的维护。

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

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-16
      • 2012-02-25
      • 1970-01-01
      • 2011-06-26
      • 2022-07-13
      • 1970-01-01
      • 2016-07-09
      • 1970-01-01
      相关资源
      最近更新 更多