【问题标题】:Read Karate config from YAML从 YAML 读取空手道配置
【发布时间】:2018-03-23 08:48:38
【问题描述】:

我想在 .yml/.yaml 文件中定义特定于环境的属性。因此我创建了以下test.yaml

baseUrl:  'http://localhost:1234'

接下来,我写了这个karate-config.js

function() {
  var env = karate.env;
   if (!env) {
     env = 'test'; // default is test
   }
   // config =  read(env + '.yaml')
   var config = read('/home/user/git/karate-poc/src/test/java/test.yaml');
   // var config = read('test.yaml');
   // var config = read('classpath:test.yaml');
  return config;
}

正如这里看到的https://github.com/intuit/karate#reading-files read() 函数应该被空手道知道,但是我不确定这是否仅适用于.feature 文件或karate-config.js

不幸的是,以上read()s 都不起作用,因为我收到了这个错误:

Caused by: com.intuit.karate.exception.KarateException: javascript function call failed: could not find or read file: /home/user/git/karate-poc/src/test/java/test.yaml, prefix: NONE
    at com.intuit.karate.Script.evalFunctionCall(Script.java:1602)

我确定该文件存在并且可读。

是我做错了什么还是我的方法不受支持?如果不支持,那么推荐的从 YAML 文件(一次)读取基于环境的配置以便在(多个).feature 文件中使用它的方法是什么?

非常感谢

编辑:项目的树形结构:

.
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradle.properties
├── gradlew
├── gradlew.bat
└── src
    └── test
        └── java
            ├── karate
            │   └── rest
            │       ├── rest.feature
            │       └── RestRunner.java
            ├── karate-config.js
            └── test.yaml

使用./gradlew test运行

【问题讨论】:

    标签: karate


    【解决方案1】:

    在 JS 中,使用 karate 对象,这里解释:https://github.com/intuit/karate#the-karate-object

    所以这应该有效:

    var config = karate.read('classpath:test.yaml');
    

    【讨论】:

    • 非常感谢,彼得。我现在使用config = karate.read('classpath:' + env + '.yaml'),它的工作原理完全符合预期。我感谢并钦佩您为空手道所做的工作。
    猜你喜欢
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    • 2022-12-20
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多