Karate不返回在“called”功能中定义的所有变量(使用def)。

huangapple go评论56阅读模式
英文:

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 &#39;called&#39; script would be returned as &#39;keys&#39; 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 &quot;isolate&quot; scenarios if you don&#39;t like splitting scenarios into different files.


</details>



huangapple
  • 本文由 发表于 2023年6月29日 23:36:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定