I am doing a simple rest api test in Cucumber Java. The response is in Json format.
The gherkin feature file i write looks like:
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"}
]}
"""
Now since there are multiple query to test with different parameters like "employees", "departments".. etc, it's natural to write Scenario Outline to perform the task:
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"|
where json_string_expected_for_employee is just:
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
by copy and paste.
But there are problems with this approach:
What is a good approach to do this? Can a string variable be defined else where in feature file to store long strings, and this variable name placed in the table?
Or any solutions? This must be a common scenario for people to compare non-trivial data output in Cucumber.
Thanks,
For your problem 1
You have to use escape character backslash
(\
)
example: \"employees\"
instead of "employees"
For your problem 2
It is common that if your input is not in the similar length of a character, it will be messy. You can use indent
to make it clear.
or
Use separate java file
to store all the input as variable and pass it to scenario outline examples
while execution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With