Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use something like a doc string in Scenario outline in Gherkin?

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:

  1. There are special characters in Json string need to escape if just surrounded by " "
  2. The Scenario Outline table looks very messy

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,

like image 389
user1559625 Avatar asked Oct 30 '22 12:10

user1559625


1 Answers

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.

like image 121
Aravin Avatar answered Nov 08 '22 09:11

Aravin