如何使用Bottle框架进行单元测试

时间:2014-12-04 22:32:23

标签: python unit-testing python-2.7 bottle

我有一些我需要测试的API端点,我不知道从哪里开始。我正在使用Bottle框架,我测试的方法之一是从请求中读取参数。如何在测试环境中模拟这个?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

如果要访问正常的瓶子语法,请使用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()
相关问题