我们有不同的测试环境。我们的要求是从karate-config-stagging.js文件中获取值(例如baseUrl),而不是每次从karate-config.js中检索值时。
src/main/java
|
\--com.karate.feature
+-- UserInfoFeatureFile.feature
\--com.karate.runner
+-- RunKarateTest.java
src/main/resources
|
+-- karate-config.js
+-- karate-config-stagging.js
UserInfoFeatureFile-
Background:
* url baseUrl
Scenario Outline: Fetch user info with user id
Given path 'users/info'
And header Authorization = 'Bearer Token'
And request {name:'<name>'}
When method GET
Then status 200
And match $ == {length:'<length>'}
Examples:
| name | length |
| Tim | 3 |
RunKarateTest-
@CucumberOptions(
features = "classpath:com/karate/feature"
)
public class RunKarateTest {
@BeforeClass
public static void before() {
System.setProperty("karate.env", "stagging");
}
}
karate-config-stagging.js-
var config = {};
config.baseUrl = 'https://baseURLofStagging/';
config.authServiceUrl = '/auth-service/';
config.userServiceUrl = '/user-service/' ;
config.abc= 'def';
return config;
}
karate-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',
baseUrl: 'https://oldbaseURL/',
authServiceUrl : '/auth-service/',
userServiceUrl : '/user-service/',
key : '',
secret : '',
authType : 'Basic'
};
return config;
}
运行测试时,尽管我们将环境设置为stagging,但它将以oldbaseURL而不是baseURLofStagging检索baseUrl。