【问题标题】:Karate - How to import json data空手道 - 如何导入 json 数据
【发布时间】:2017-10-31 12:47:02
【问题描述】:

我想将一些 JSON 数据导入到我的测试中。
为了记录,我应该这样做:

* def data = read('classpath:init/data.json')

我已经创建了包含以下内容的 JSON 文件:

{
    "name": "ehsan"
}

这是我的代码:

  Background:
    * def data = call read('classpath:init/data.json')

  Scenario:
    * print data

但它什么也没打印并说:

16:11:30.898 [main] WARN com.intuit.karate - not a js function or feature file: read('classpath:init/data.json') - [type: JSON, value: com.jayway.jsonpath.internal.JsonContext@7d61eccf]

【问题讨论】:

    标签: json unit-testing karate


    【解决方案1】:

    以下代码正确:

    * def data = read('classpath:init/data.json')
    

    只有你必须删除 [call]

    【讨论】:

      【解决方案2】:

      是的,请仔细阅读错误消息(和文档) - 在您 read 的那一刻,“调用” JSON 文件没有任何意义 - 您已经有了可重复使用的数据。只需这样做:

      Background:
          * def data = read('classpath:init/data.json')
      
        Scenario:
          * print data
      

      【讨论】:

      • 请在此上下文中提出另一个问题:如何导入我们在项目中拥有的所有 javascript 文件?
      • @YounessMourtaji 永远不会被支持
      • 所以我们需要在一个变量中导入每个需要的javascript才能使用它?
      • @YounessMourtaji 正确,否则会出现维护问题。如果你真的想要这种东西,你总是可以为你的项目编写一个小实用程序。万事如意
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多