【问题标题】:Karate API function/keyword to substitute JSON placeholder key with argument passed空手道 API 函数/关键字用传递的参数替换 JSON 占位符键
【发布时间】:2019-05-29 17:51:23
【问题描述】:

在使用空手道 API 的 Web 服务测试中,如何将 JSON 键替换为我在后台定义的值?

背景:我正在通过空手道 API 访问 REST API 端点。这给我返回了一个深层嵌套的 JSON。一旦我得到响应,我需要评估响应是否包含我的参数作为键以及键的值是否不为空。下面是我的代码

Background:
* def execParam = 'ArgumentProvided'

@MyTestCase
Given url myUrl + '/' + execParam
When method get
Then status 200
And match response.subElement contains {execParam : '#notnull'}

使用上面的代码,断言失败并出现错误

expected: {execParam:'#notnull'}, reason: actual value does not contain expected

我的期望是代码断言{'ArgumentProvided':'#notnull'}。代码当前断言{execParam:'#notnull'}

我尝试将execParam 替换为#execParam$.execParam#[$.execParam],但这些都不能替换该值。所以需要帮助替换 JSON 密钥

【问题讨论】:

    标签: karate


    【解决方案1】:

    对于这个有趣的用例,您可以像这样动态构建预期的 JSON:

    * def execParam = 'ArgumentProvided'
    * def response = { subElement: { ArgumentProvided: 'foo' } }
    * def expected = {}
    * eval expected[execParam] = '#notnull'
    * match expected == { ArgumentProvided: '#notnull' }
    * match response.subElement contains expected
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-01
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多