Karate - Not able to run dynamic scenario outline

2019-08-10 16:17发布

问题:

Here is my feature file , which just loads the json file and wants to iterate over the same

 Background:
 * def kittens = read('../json/test.json')
 Scenario Outline: cat name: <name>
 * print <name>
  Examples:
  | name |
  | kittens |

Here is the output

[
  {
    "name": "Bob"
  },
  {
    "name": "Wild"
  },
  {
    "name": "Nyan"
  },
  {
    "name": "Keyboard"
  },
  {
    "name": "LOL"
  },
  {
    "name": "Ceiling"
  }
]

As per my understanding this should run 7 times and give me individual variable values , But its running only once and giving me full json as output .

Let me know if I am missing anything.

回答1:

You are passing the list/array with a variable name in it, it will run only once as it interprets your entire json data as single variable name.

you could have noted it printed the entire data in your test.json once, as it acted as normal scenario outline.

You should pass the array directly as below to make it as dynamic scenario outline.

Feature: Dynamic Scenario Outline
 Background: 
  * def kittens = read('../json/test.json')
 Scenario Outline: cat name: <name> 
  * print <name> 
 Examples: 
  | kittens |

For dynamic scenario outline, the variables <name> will actually derived from your json, if there is key in your json as "name". Not as the header of the list in Examples:.

Karate docs- Dynamic Scenario Outline



标签: karate