简体   繁体   English

有没有办法在空手道中查询 csv 文件?

[英]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.我正在寻找类似的功能,例如Fillo Excel API,我们可以在 excel 文件中使用类似查询语句进行 CRUD 操作。

A select statement in a csv file is a great addition to the framework to provide more flexibility in test data driven approach testing. csv 文件中的 select 语句是对框架的重要补充,可在测试数据驱动的方法测试中提供更大的灵活性。

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.而不是像这样将所有测试数据放在 1 行或 1 个单元格中,并在处理之前进行字符串拆分。

|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')我们可以像下面这样设计我们的 csv 文件,当我们有类似* 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|

There's no need.没有必要。 Karate can transform a CSV file into a JSON array in one line:空手道可以在一行中将 CSV 文件转换为 JSON 数组:

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

After that just use JsonPath or a "filter" operation to "query" for data (search the docs for more examples):之后,只需使用 JsonPath 或“过滤器”操作来“查询”数据(搜索文档以获取更多示例):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM