mrapp2
mrapp2

Reputation: 71

Karate - Ability to execute tests on a specific set of data in a csv file

I am on a team, presenting the advantages of Karate to move forward as the framework of choice for our API testing. However, I have come across a couple questions, in regards to data-driven testing.

I have gone through the documentation, csv files and cannot find a solution for this question:

  1. Is Karate capable of executing tests on specific data sets (For instance, based on priority p0, p1) given in a csv file?

Example "test.csv":

|priority|data1|
| p0     |  1  |  
| p0     |  2  |
| p1     |  4  |
| p1     |  6  |

I want to run my test cases with specific data sets in a csv file (p0, or p1, or both). Is Karate capable of this?

Upvotes: 1

Views: 1501

Answers (2)

Ari Singh
Ari Singh

Reputation: 1306

Another useful feature of karate that you can use here is to tag your different segments of example data, and run the scenario with one or more tags, depending on your needs. Refer to: https://intuit.github.io/karate/#tags-and-examples

So you could write your example data as (a scenario can have multiple "Examples:" section, and by default a scenario will run all the "Examples: " unless tags are specified during the execution of the scenario:

@priority=p0
Examples:
  |data1|
  |  1  |  
  |  2  |

@priority=p1
Examples:
  |data1|
  |  4  |
  |  6  |

Now you can run your scenario with both p0 and p1 tags, and it will go thru data1 : 1, 2, 4, 6. Or run with only one tag - and it will run the scenario with that tag's data.

Upvotes: 0

Peter Thomas
Peter Thomas

Reputation: 58088

There are multiple ways I would do this, here is one:

Background:
* def data = read('test.csv')
* def selected = 'p1'
* def fun = function(x){ return x.priority == selected }
* def filtered = karate.filter(data, fun)

Scenario Outline:
* print __row

Examples:
| filtered |

You don't need to force yourself into a Scenario Outline, you can loop over data and ignore the rows where you don't want to do any processing.

Refer to this answer for more ideas: https://stackoverflow.com/a/61685169/143475

Note that you can "fall back" to Java for advanced logic if needed: https://github.com/intuit/karate#calling-java

Upvotes: 2

Related Questions