我正在用 Cucumber Java 做一个简单的 rest api 测试。响应采用 Json 格式。
我写的小 cucumber 特征文件看起来像:
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"}
]}
"""
现在,由于有多个查询要使用不同的参数(如“员工”、“部门”等)进行测试,因此很自然地编写 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"|
其中 json_string_expected_for_employee 只是:
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
通过复制和粘贴。
但是这种方法存在问题:
这样做的好方法是什么?是否可以在特征文件中的其他位置定义字符串变量来存储长字符串,并将此变量名称放在表中?
或者有什么解决办法?这一定是人们在 Cucumber 中比较非平凡数据输出的常见场景。
谢谢,
最佳答案
对于您的问题 1
您必须使用转义字符 backslash
( \
)
例如:\"employees\"
而不是 "employees"
对于您的问题 2
通常,如果您的输入不是一个字符的相似长度,它会很困惑。您可以使用 indent
说清楚。
或者
使用单独的 java file
将所有输入存储为变量并将其传递给 scenario outline examples
执行时。
关于json - 如何在 Gherkin 的场景大纲中使用类似文档字符串的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34387947/