【问题标题】:How to unit test using Bottle framework如何使用 Bottle 框架进行单元测试
【发布时间】:2016-12-22 03:08:02
【问题描述】:

我有一些 API 端点需要测试,但我不确定从哪里开始。我正在使用 Bottle 框架,并且我正在测试的一种方法从请求中读取参数。如何在测试环境中进行模拟?

【问题讨论】:

标签: python unit-testing python-2.7 bottle


【解决方案1】:

Bottle 没有 Flask (see here) 的一些测试细节。但 Bottle 建议使用 WSGI 测试工具和常规单元测试框架 (http://bottlepy.org/docs/dev/recipes.html#functional-testing-bottle-applications)。您将无法访问瓶子语法或传递给模板的参数等,但您也不必实际运行单独的服务器。

【讨论】:

【解决方案2】:

如果您想访问正常的瓶子语法,请使用boddle 进行单元测试。示例:

import bottle, unittest
from boddle import boddle


@bottle.get('/woot')
def woot():
  return bottle.request.params['name']


class TestIt(unittest.TestCase):
  def testWoot(self):
    with boddle(params={'name':'derek'}):
      self.assertEqual(woot(), 'derek')


if __name__=='__main__':
  unittest.main()

【讨论】:

    猜你喜欢
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多