从YAML读取空手道配置 [英] Read Karate config from YAML
问题描述
我想在 .yml
/ .yaml
文件中定义特定于环境的属性.因此,我创建了以下 test.yaml
:
I would like to define environment-specific properties in a .yml
/.yaml
file. Therefore I created the following 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
.
As seen here https://github.com/intuit/karate#reading-files the read()
function should be known by Karate, however I'm not sure if this only applies to .feature
files or the karate-config.js
too.
不幸的是,上述 read()
都不起作用,因为出现了此错误:
Unfortunately, none of the above read()
s work, as I'm getting this error:
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)
我确定该文件存在并且可读.
I'm sure that the file exists and is readable.
我做错了什么还是不支持我的方法?如果不支持,那么建议一次从YAML文件读取基于环境的配置的推荐方法,以便在(多个) .feature
文件中使用它?
Am I doing something wrong or is my approach not supported? If it's not supported, what would be the recommended way to read the configuration based on the environment from a YAML file (once) in order to use it in (multiple) .feature
files?
非常感谢您
项目的树结构:
.
├── 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测试
推荐答案
In JS, use the karate
object, which is explained here: https://github.com/intuit/karate#the-karate-object
所以这应该起作用:
var config = karate.read('classpath:test.yaml');
这篇关于从YAML读取空手道配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!