Search code examples
apicsvkaratedataprovider

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?


Solution

  • 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