1

I have a use case where I want to assert on a API response and compare it with the csv data.

Step1:

Csv file: *test.csv*
id,date,fullname,cost,country,code
1,02-03-2002,user1,$200,Canada,CAN
2, 04-05-2016,user2,$1500,United States, USA

I read the csv file and store it in a variable

  • def var1 = read(test.csv)

So now, var1 is a list of jsons based on my csv

var1 = [
{
"id":1,
"date":"02-03-2002",
"fullname": "user1",
"cost": "$200",
"country": "Canada",
"code": "CAN"
},
{
"id":2,
"date":"04-05-2016",
"fullname": "user2",
"cost": "$1500",
"country": "United States",
"code": "USA"
}
]

Step2: I hit my api and get a response

Given url "https://dummyurl.com
Given path "/userdetails"
When method get
Then status 200
* def apiResponse = response

Step 3: My api returns a list response which is:

{
"id":1,
"date":"02-03-2002",
"fullname": "user1",
"cost": "$200",
"country": { 
  "name": "Canada",
  "code": "CAN"
  }
},
{
"id":2,
"date":"05-04-2012",
"fullname": "user2",
"cost": "$1500",
"country": { 
  "name": "United States",
  "code": "USA"
  }
},
...and more 100 records..
]

Step 4: So there are two assertions now which I wanted to perform

  1. Get the count of csvresponse and apiresponse and compare which I did using the .length operator

  2. Secondly, I want to confirm if each csv records are matching with each api response. And if possible in my case id key from csv and apiresponse is primary key, so if I can iterate on id and match the api response for any discrepancy.

Let me know if this is readable for you and if I was able to explain my use case. Thanks for your earlier response.

Nachi K
  • 45
  • 6

1 Answers1

1

Please read up on the match contains syntax, that's all you need: https://github.com/intuit/karate#match-contains

So this one line should be enough:

* match var1 contains response

Also look at this answer in case the new contains deep helps: https://stackoverflow.com/a/63103746/143475

Try to avoid iterating, it is not needed for most API tests. But you can certainly do it. Look at these answers:

https://stackoverflow.com/a/62567262/143475

Also read this - because I suspect you are trying to over-complicate your tests. Please don't. Write tests where your are 100% sure of the "shape" of the response as far as possible: https://stackoverflow.com/a/54126724/143475

And please please read the docs. It is worth it.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    Thanks Peter for your prompt response, I am surely going through the documentation and will try looking at all the links you shared. – Nachi K Jul 28 '20 at 17:41
  • match is not working for me as my csv data is not nested and my api response has a nested json. I am thinking to try having some logic – Nachi K Jul 29 '20 at 17:26
  • 1
    @NachiK look, this is exactly what I was trying to tell you when I marked your first question as duplicate: https://stackoverflow.com/a/62449166/143475 - all the best – Peter Thomas Jul 29 '20 at 17:28
  • But may be I am getting it wrong or unable to explain you, about my comparison of two list based on conditional logic.. may be I will give it a shot with some function. Thanks for your help – Nachi K Jul 29 '20 at 17:38
  • I tried with your suggestion of reading a CSV - reading csv is giving "\" in the response since my csv has nested option, is there any way I can remove it using a replace option. – Nachi K Jul 31 '20 at 19:15
  • @NachiK no. don't use csv unless you know what you're doing – Peter Thomas Jul 31 '20 at 19:26
  • I am trying to read a CSV and compare with a api list response, the csv and api both have same fields. Both have nested fields, but when I read my csv and print the response - I get like this ["id":"123", "name":"test","data":"{\"status\":\"Yes\", \"code\":\"Red\"}"]. My csv is: id, name,data ---- first row is: 123,test,"{""Status"":""Yes"",""code"":""Red""}" – Nachi K Jul 31 '20 at 20:06
  • @NachiK the linked answer says a) don't use csv for nested structures b) how to escape nested content if needed. if this is not possible, please look for another framework. I have nothing more to add – Peter Thomas Aug 01 '20 at 01:55