如何在 Gherkin 的场景大纲中使用类似文档字符串的内容?

use*_*625 5 json cucumber gherkin cucumber-java

我正在用 Cucumber Java 做一个简单的 rest api 测试。响应采用 Json 格式。

我写的小黄瓜特征文件看起来像:

  Scenario:  
    Given I query service by "employees"
    When I make the rest call
    Then response should contain:
      """
      {"employees":[
      {"firstName":"John", "lastName":"Doe"},
      {"firstName":"Anna", "lastName":"Smith"},
      {"firstName":"Peter", "lastName":"Jones"}
      ]}
      """
Run Code Online (Sandbox Code Playgroud)

现在,由于有多个查询要使用不同的参数(如“员工”、“部门”等)进行测试,因此很自然地编写 Scenario Outline 来执行任务:

  Scenario Outline: 
    Given I query service by "<category>"
    When I make the rest call
    Then response should contain "<json_string_for_that_category>"
    Examples:
      | category     | json_string_for_that_category     |
      | employee     | "json_string_expected_for_employee"  |
      | department   | "json_string_expected_for_department"|
Run Code Online (Sandbox Code Playgroud)

其中 json_string_expected_for_employee 只是:

  {"employees":[
  {"firstName":"John", "lastName":"Doe"},
  {"firstName":"Anna", "lastName":"Smith"},
  {"firstName":"Peter", "lastName":"Jones"}
  ]}
Run Code Online (Sandbox Code Playgroud)

通过复制和粘贴。

但是这种方法存在问题:

  1. Json字符串中有特殊字符需要转义如果只是用“”
  2. Scenario Outline 表看起来很乱

这样做的好方法是什么?是否可以在特征文件中的其他位置定义字符串变量来存储长字符串,并将此变量名称放在表中?

或者有什么解决办法?这一定是人们在 Cucumber 中比较非平凡数据输出的常见场景。

谢谢,

Ara*_*vin 1

对于你的问题1

您必须使用转义字符backslash( \)

示例:\"employees\"代替"employees"

对于你的问题2

通常情况下,如果你输入的字符长度不相似,就会很混乱。您可以使用indent它来表达清楚。

或者

使用分离java file将所有输入存储为变量并scenario outline examples在执行时将其传递给。