英文:
karate does not return all variables that were defined (using def) in the 'called' feature
问题
我有以下文件 karate-config-test.js
function fn() {
var env = karate.env; // 获取系统属性 'karate.env'
var baseUrl = "http://localhost:5000"
var config = {
env: env,
baseUrl: baseUrl
};
// 打开连接到消息代理(mqtt, amqp)
var async_message = karate.callSingle('classpath:com/connect/apitests/commons/async_message/async_message.feature', config);
config.mqtt = async_message.mqtt
config.amqp = async_message.amqp
return config;
}
以及以下特性 async_message.feature
@ignore
Feature: 实例化异步消息代理的实用工具
Background:
* url baseUrl
Scenario: 设置 AmqpUtils 和 MqttUtils 类的实例
* def amqp = 'pippo'
Scenario: 设置 MqttUtils 类的实例
* def mqtt = 'pluto'
当我检查变量 **async_message 的值时,我只看到了一个场景的变量,而不是两个场景的变量,例如我只看到了 mqtt。为什么会发生这种情况?在 karate 文档中我读到
- 在 'called' 脚本中定义的所有变量(使用 def)将作为 JSON 类似对象中的 'keys' 返回。
<details>
<summary>英文:</summary>
I have the following file karate-config-test.js
function fn() {
var env = karate.env; // get system property 'karate.env'
var baseUrl = "http://localhost:5000"
var config = {
env: env,
baseUrl: baseUrl
};
//open connection to message broker (mqtt, amqp)
var async_message = karate.callSingle('classpath:com/connect/apitests/commons/async_message/async_message.feature', config);
config.mqtt = async_message.mqtt
config.amqp = async_message.amqp
return config;
}
And the following feature async_message.feature
@ignore
Feature: utility that instance async message broker
Background:
- url baseUrl
Scenario: Set instance of a class AmqpUtils And MqttUtils
* def amqp = 'pippo'
Scenario: Set instance of a class MqttUtils
* def mqtt = 'pluto'
when i check the value of the variable **async_message i see the variables of one scenario but not both**, for example I saw only mqtt.
Why is this happening?
In karate documentation I read
- All variables that were defined (using def) in the 'called' script would be returned as 'keys' within a JSON-like object.
</details>
# 答案1
**得分**: 1
Karate 的设计是这样的,当你执行任何类型的 `call` 时,最后一个 `Scenario` 被视为返回变量的依据。
换句话说,最佳实践是一次调用一个 `Scenario`。如果你不喜欢将场景拆分到不同的文件中,你可以使用标签来“隔离”场景。
<details>
<summary>英文:</summary>
Karate is designed so that when you do a `call` of any kind, the last `Scenario` is what is considered for returning variables.
In other words, the best practice is to call one `Scenario` at a time. You can use tags to "isolate" scenarios if you don't like splitting scenarios into different files.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论