Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

automated tests - Karate: Using data-driven embedded template approach for API testing

I want to write data-driven tests passing dynamic values reading from external file (csv). Able to pass dynamic values from csv for simple strings (account number & affiliate id below). But, using embedded expressions, how can I pass dynamic values from csv file for "DealerReportFormats" json array below?

Any help is highly-appreciated!!

Scenario Outline: Dealer dynamic requests
    Given path '/dealer-reports/retrieval'
    And request read('../DealerTemplate.json')   
  When method POST
    Then status 200
    Examples: 
      | read('../DealerData.csv') | 


DealerTemplate.json is below
{    
  "DealerId": "FIXED",
  "DealerName": "FIXED",
  "DealerType": "FIXED",

  "DealerCredentials": {
    "accountNumber": "#(DealerCredentials_AccountNumber)",
    "affiliateId": "#(DealerCredentials_AffiliateId)"
  },
"DealerReportFormats": [
    {
      "name": "SalesReport",
      "format": "xml"
    },
    {
      "name": "CustomerReport",
      "format": "txt"
    }
  ]
}

DealerData.csv: 

DealerCredentials_AccountNumber,DealerCredentials_AffiliateId
testaccount1,123
testaccount2,12345
testaccount3,123456

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

CSV is only for "flat" structures, so trying to mix that with JSON is too ambitious in my honest opinion. Please look for another framework if needed :)

That said I see 2 options:

a) use proper quoting and escaping in the CSV

b) refer to JSON files

Here is an example:

Scenario Outline:
* json foo = foo
* print foo

Examples:
| read('test.csv') |

And test.csv is:

foo,bar
"{ a: 'a1', b: 'b1' }",test1
"{ a: 'a2', b: 'b2' }",test2

I leave it as an exercise to you if you want to escape double-quotes. It is possible.

Option (b) is you can refer to stand-alone JSON files and read them:

foo,bar
j1.json,test1
j2.json,test2

And you can do * def foo = read(foo) in your feature.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...