You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Karate的线索CRUD操作:CSV行匹配与工作流连贯问题咨询

基于Karate的线索CRUD测试问题与解决方案

问题1:如何确保Feature执行时,创建与更新所用CSV的第一行匹配使用?

  • 加载两个CSV文件为列表,通过索引直接关联对应行:
    # 在Feature开头加载CSV数据
    * def createData = read('classpath:create-data.csv')
    * def updateData = read('classpath:update-data.csv')
    
    # 取第一行数据(Karate中列表索引从0开始)
    * def currentCreateRow = createData[0]
    * def currentUpdateRow = updateData[0]
    
  • 如果需要批量执行多组数据,通过索引循环配对:
    # 生成与CSV行数匹配的索引列表
    * def indices = karate.range(0, createData.length)
    # 调用子Feature处理每一组配对数据
    * call read('lead-crud-flow.feature') indices
    
    子Feature中通过__arg获取当前索引,取对应行:
    * def createRow = createData[__arg]
    * def updateRow = updateData[__arg]
    

问题2:如何确保从CSV创建、检索、CSV更新到再次检索的全流程连贯执行?

将完整流程封装为线性执行的Feature,通过变量传递关键上下文数据(如线索ID、版本号),步骤如下:

  1. 创建线索:用CSV创建数据填充请求,发送POST并提取线索ID

    Given url 'https://your-api-domain/leads'
    And request { name: '#(currentCreateRow.name)', email: '#(currentCreateRow.email)' }
    When method post
    Then status 201
    * def leadId = response.id  # 存储线索ID,用于后续步骤
    
  2. 检索线索并提取更新关键信息:用线索ID发送GET,提取更新所需字段(如乐观锁版本号)

    Given url 'https://your-api-domain/leads/' + leadId
    When method get
    Then status 200
    * def leadVersion = response.version  # 存储版本号,避免更新冲突
    
  3. 更新线索:结合CSV更新数据与提取的关键字段,发送PUT请求

    Given url 'https://your-api-domain/leads/' + leadId
    And request { id: '#(leadId)', version: '#(leadVersion)', phone: '#(currentUpdateRow.phone)', status: '#(currentUpdateRow.status)' }
    When method put
    Then status 200
    
  4. 再次检索验证:重新获取线索,验证更新后的数据符合预期

    Given url 'https://your-api-domain/leads/' + leadId
    When method get
    Then status 200
    And match response.phone == currentUpdateRow.phone
    And match response.status == currentUpdateRow.status
    
  • 批量执行时,通过call循环每一组CSV配对行,Karate会为每一次调用创建独立的上下文,避免变量污染,保证每组流程的连贯性。

内容的提问来源于stack exchange,提问作者bulbasorus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 16:23:12