将变量传递到示例部分 [英] Passing variables into Examples section
本文介绍了将变量传递到示例部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
* def
表达式"导出"它们。现在我想在另一个功能中使用这些变量
Scenario Outline: Successful transfer from <fromLocation> to <toLocation>
* call read('re-usable.feature')
Given request read('request.json')
When method post
Then status 200
Examples:
| fromLocation | toLocation |
| cityA | cityB |
| cityB | cityA |
请求:
{
source: "#(fromLocation)"
destination: "#(toLocation)"
}
我的方案失败了,因为在请求中设置了CITYA/CITYB,而不是以这些名称存储的值,我尝试使用空手道配置文件中的变量,甚至
Scenario Outline: Successful transfer from <fromLocation> to <toLocation>
* def res = call read('re-usable.feature')
Given request read('request.json')
When method post
Then status 200
Examples:
| fromLocation | toLocation |
| res.cityA | res.cityB |
| res.cityB | res.cityA |
传递的是‘res.cityA’、‘res.ityyB’而不是纽约/拉斯维加斯
此外,我还尝试了以下方法
Background:
* call read('re-usable.feature')
Scenario Outline: Successful transfer from <fromLocation> to <toLocation>
Given request read('request.json')
When method post
Then status 200
Examples:
| fromLocation | toLocation |
| cityA | cityB |
| cityB | cityA |
和
Background:
* def res = call read('re-usable.feature')
Scenario Outline: Successful transfer from <fromLocation> to <toLocation>
Given request read('request.json')
When method post
Then status 200
Examples:
| fromLocation | toLocation |
| res.cityA | res.cityB |
| res.cityB | res.cityA |
所有提供的案例都返回相同的变量名称,而不是值。例如,如果我在请求中设置了#(cityA)
而不是#(toLocation)
,则一切都如预期,但在这种情况下我无法测试其他场景
@PeterThomas您将我的上一个问题标记为重复,但您提供的链接与我的问题无关;)
替换
Examples
中的变量推荐答案不受支持。此外,您似乎混淆了fromLocation
和cityA
,我强烈建议测试保持简单,因为我linked to from your previous question。
无论如何,尤其是当您使用call
生成数据时,最好先使用table
,然后使用循环:
* def data = [{ cityA: 'a', cityB: 'b' }, { cityA: 'c', cityB: 'd' }]
* call read('re-usable.feature') data
所以请阅读:https://github.com/intuit/karate#data-driven-features,您仍然需要2个功能文件,但执行顺序是"颠倒"的。
否则,我还是看不懂你的试卷。解决Examples
表的单元格缺乏"动态化"问题的唯一方法如下:
Scenario Outline:
* def res = { cityA: 'foo' }
* def value = <fromLocation>
* match value == 'foo'
Examples:
| fromLocation |
| res.cityA |
Refer:examples.feature
-最后一个示例。
但希望您能理解,您使用Examples
的方式是错误的,这会使将来需要阅读您的测试的所有人感到困惑。如果您仍然有问题,问一个新的问题--这一次给出一些您试图自动化的用例的摘要。
这篇关于将变量传递到示例部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文