2

我是 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 返回给生菜,或者生菜将如何运行测试?我浏览了生菜文档,但仍然无法弄清楚如何对我的 REST API 进行自动化测试。任何建议或支持将不胜感激。在此先感谢您的时间。

4

1 回答 1

0

实际上你不需要直接调用 @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

于 2016-03-11T13:21:57.203 回答