空手道:如果在 'stand-alone' 模式下包含 __arg 和 运行,场景将失败

Karate: Scenario fails if contains __arg and run in 'stand-alone' mode

当我尝试 运行 包含内置 __arg 变量的场景作为 'stand-alone'(不是 'called')时,我遇到了问题,然后我的测试失败了有一个错误(我不 @ignore 被调用的,因为为了在 'called' 和 'stand-alone' 模式下使用它):

evaluation (js) failed: __arg, javax.script.ScriptException: ReferenceError: "__arg" is not defined in <eval> at line number 1
stack trace: jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)

以下两个简单的特征应该足以重现。

叫-standalone.feature:

Feature: Called + Stand-alone Scenario
  Scenario: Should not fail on __arg when run as stand-alone
    * def a = __arg
    * print a

caller.feature:

Feature: Caller
  Scenario: call without args
    When def res = call read('called-standalone.feature')
    Then match res.a == null

  Scenario: call with args
    When def res = call read('called-standalone.feature') {some: 42}
    Then match res.a == {some: 42}

将这两个特性放入骨架工程中运行mvn test会报错

我希望这应该能正常工作,因为文档说“‘调用的’空手道脚本......在‘独立’模式下可以表现得像‘普通’空手道测试”。

‘called’ Karate scripts don’t need to use any special keywords to ‘return’ data and can behave like ‘normal’ Karate tests in ‘stand-alone’ mode if needed

所有空手道变量都必须在 运行 时“定义”。这是一个不能放松的规定。

所以你应该重新设计你的脚本。好处是您可以使用 karate.get() 设置“默认值”。

* def a = karate.get('__arg', null)

这应该可以回答您的问题。