编写用于测试REST API的Cucumber场景

时间:2017-09-27 09:46:06

标签: rest cucumber cucumber-jvm

我是Cucumber Testing框架的新手,我应该使用黄瓜来测试REST API。

为了我们的理解,假设有一个端点为http://localhost:8080/REST/coffee/search的REST API 并接受三个查询参数" type"," toppings"和"成本"。这种查询参数有几种组合是可能的。

我的问题在于编写方案来测试此API。我想通过使用这些查询参数的几种组合来调用API。 但我对使用数据表或场景大纲感到困惑。

With Data Tables: //data is sent inside StepDefinition method as Data Table 

    Scenario: User Searches for Coffee
        Given Coffee exists with valid data 
        |type         |toppings      |  cost                |
        |Latte        |              |       1              |
        |Espresso     |Whipped Cream |       2              |       
        |Espresso     |              |       3.5            |
        When User call the API with given data
        Then Verify API Response for status

With Scenario Outline: // data is sent to individual params of stepdefinition.  

    Scenario Outline: User Searches for Coffee
        Given Coffee exists with valid data <type>, <toppings>, <cost>
        When User call the API with given data
        Then Verify API Response for status

    Examples:   

        |type         |toppings      |  cost                |
        |Latte        |              |       1              |
        |Espresso     |Whipped Cream |       2              |       
        |Espresso     |              |       3.5            |

是否可以在场景轮廓中获取数据表,如果我尝试下面的内容?

Scenario Outline: User Searches for Coffee
        Given Coffee exists with valid data 
        When User call the API with given data
        Then Verify API Response for status

    Examples:   

        |type         |toppings      |  cost                |
        |Latte        |              |       1              |
        |Espresso     |Whipped Cream |       2              |       
        |Espresso     |              |       3.5            |



Since I have 10+ query params to use, Please suggest some best practices too.       

2 个答案:

答案 0 :(得分:0)

您最好编写单元测试来进行此类测试。然后,您可以将查询参数存储在数据结构中并迭代它。

答案 1 :(得分:0)

您的要求在您的问题中并不完全清楚,但您可能正在寻找的是您也可以在步骤的数据表中使用示例数据:

Scenario Outline: User Searches for Coffee
   Given Coffee exists with:
     | type   | toppings  | cost   |
     | <type> | <topping> | <cost> |
    When User call the API with given data
    Then Verify API Response for status
Examples:   
  | type         | topping       | cost |
  | Latte        |               | 1    |
  | Espresso     | Whipped Cream | 2    |       
  | Espresso     |               | 3.5  |