繁体   English   中英

如何在 JUnit 测试中访问 Karate 配置参数?

[英]How to access Karate config parameters in JUnit tests?

有没有办法在 JUnit 测试中从 karate-config.js 访问配置参数?

例子:

空手道-config.js

function fn() {   
  var env = karate.env; // get java system property 'karate.env'
  karate.log('karate.env system property was:', env);
  if (!env) {
    env = 'dev'; // a custom 'intelligent' default
  }
  var config = { // base config JSON
    appId: 'my.app.id',
    appSecret: 'my.secret',
    someUrlBase: 'https://some-host.com/v1/auth/',
    anotherUrlBase: 'https://another-host.com/v1/'
  };
  if (env == 'stage') {
    // over-ride only those that need to be
    config.someUrlBase = 'https://stage-host/v1/auth';
  } else if (env == 'e2e') {
    config.someUrlBase = 'https://e2e-host/v1/auth';
  }
  // don't waste time waiting for a connection or if servers don't respond within 5 seconds
  karate.configure('connectTimeout', 5000);
  karate.configure('readTimeout', 5000);
  return config;
}

我的测试程序

public class MyTest {

    @Test
    public void test() {
        // How to access e.g. config.appId?
    }
}

如果你需要从java代码调用外部javascript函数,我建议你看看这个

但为什么 !?

有多种方法,但首先 - 也许您过度设计了一些东西,并注意可以在空手道中读取*.properties文件: properties.feature

您还可以使用单个空场景创建功能文件 - 并从 Java API 调用它: https : //github.com/intuit/karate#java-api

Map<String, Object> result = Runner.runFeature('classpath:foo.feature', null, true);

这将为您提供返回Map的 config 值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM