csvkaratedata-driven-tests

Is there a way to query a csv file in Karate?


I am looking for a similar functionality like Fillo Excel API where we can do CRUD operations in an excel file using query like statements.

A select statement in a csv file is a great addition to the framework to provide more flexibility in test data driven approach testing.

Sample scenario: A test case that needs to have multiple data preparation of inserting records to database.

Instead of putting all test data in 1 row or 1 cell like this and do a string split before processing.

|TC-ID|FNAME               |LNAME               |
|TC-1 |FNAME1,FNAME2,FNAME3|LNAME1,LNAME2,LNAME3|
|TC-2 |FNAME4              |LNAME4              |

We can design our csv file like this below, when we have something like * def data = read('Select * from persons.csv where TC-ID=TC-1')

|TC-ID|FNAME |LNAME |
|TC-1 |FNAME1|LNAME1|
|TC-1 |FNAME2|LNAME2|
|TC-1 |FNAME3|LNAME3|
|TC-2 |FNAME4|LNAME4|

Solution

  • There's no need. Karate can transform a CSV file into a JSON array in one line:

    * def data = read('data.csv')
    

    After that just use JsonPath or a "filter" operation to "query" for data (search the docs for more examples):

    * def found = data.find(x => x['TC-ID'] === 'TC-1')
    * def results = data.filter(x => x.FNAME.startsWith('A'))