如何使用 Bottle 框架进行单元测试[英] How to unit test using Bottle framework

本文是小编为大家收集整理的关于如何使用 Bottle 框架进行单元测试的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一些我需要测试的API端点,我不确定从哪里开始.我正在使用瓶子框架,并且正在测试的方法之一读取了请求的参数.如何在测试环境中模拟它?

推荐答案

使用 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()

其他推荐答案

瓶没有烧瓶(请参阅此处,请参见此处,请参阅此处,请参阅此处).但是瓶建议使用WSGI测试工具以及常规的单元测试框架( http://bottlepy.org/docs/dev/recipes.html#functional-testing-bottle-applications ).您无法访问瓶子语法或传递给模板等的参数,但是您也不必实际运行单独的服务器.

本文地址:https://www.itbaoku.cn/post/2698485.html

问题描述

I have some API endpoints that I need to test and I'm not sure where to start. I'm using the Bottle framework and one of the methods I'm testing reads a parameter off the request. How do I simulate this in a test environment?

推荐答案

Use boddle for unit testing if you want access to the normal bottle syntax. Example:

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()

其他推荐答案

Bottle doesn't have some of the testing niceties that Flask has (see here). But Bottle recommends using a WSGI Testing Tools along with a regular unit testing framework (http://bottlepy.org/docs/dev/recipes.html#functional-testing-bottle-applications). You won't have access to bottle syntax or the parameters passed to templates, etc, but you won't have to actually run a separate server either.