【问题标题】:BDD tests to test Flask REST APIBDD 测试以测试 Flask REST API
【发布时间】:2015-08-21 21:45:34
【问题描述】:

我是 python 新手,目前正在学习如何使用 Lettuce (python) 编写 BDD 测试。我有一个非常简单的基于 Flask 框架的 REST API。我在调用 app.route 下的函数时有点卡住了。例如我的 API 是这样的

@app.route('/')
@app.route('/documentation')
def documentation():
    return auto.html()

我的生菜测试位于名为 features 的文件夹中。此文件夹 features 有两个文件,称为 test.feature 和 steps.py。 test.feature 包含以下功能。

   Feature: To test the root of API

   Scenario: Call the root of the API
      Given I have "/" or "/documentation" 
      When the user requests GET '/' 
      Then I response should be "GET HTTP/1.1 200".

定义写在steps.py文件中,如下。

   @step('I have "([^"]*)" or "([^"]*)"')
   def display_api(step, value, option):
      print ('Attempting to display the API docs..')
      -----

我不确定如何调用 @app.route('/') 来运行测试并将状态 200 返回给 lettuce,或者 lettuce 如何运行测试?我浏览了生菜文档,但仍然无法弄清楚如何对我的 REST API 进行自动化测试。任何建议或支持将不胜感激。在此先感谢您的时间。

【问题讨论】:

    标签: python-2.7 rest flask bdd lettuce


    【解决方案1】:

    确实你不需要直接调用@app.route('/') 来运行测试,你只需要描述步骤和测试用例。然后运行 ​​lettuce 工具,该工具会通过测试:

        $ lettuce test/features/
    
        Feature: .... # test/features/user.feature:1
        Scenario:  ...
    

    等等。

    这里有更多信息:https://semaphoreci.com/community/tutorials/bdd-testing-a-restful-web-application-in-python

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-02
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-06
      • 2021-06-04
      相关资源
      最近更新 更多