我正在 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"}
]}
"""
现在,由于有多个查询要使用不同的参数进行测试,例如“员工”、“部门”.. 等,所以很自然地编写场景大纲来执行任务:
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"}
]}
通过复制和粘贴。
但是这种方法存在问题:
- Json字符串中有特殊字符需要转义如果只是被“”包围
- Scenario Outline 表看起来很乱
这样做的好方法是什么?是否可以在特征文件中的其他位置定义字符串变量以存储长字符串,并将该变量名放在表中?
或者有什么解决办法?这一定是人们比较 Cucumber 中非平凡数据输出的常见场景。
谢谢,