将变量传递到示例部分[重复]

2023-12-13

我设置了两个变量,假设在一个功能中 cityA = 'New York' 和 cityB = 'Las Vegas' 并将它们“导出”* 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.cityB”而不是纽约/拉斯维加斯 此外,我还尝试了以下方法

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 |

and

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 and cityA,我强烈建议保持测试简单,因为我链接到您之前的问题.

无论如何,尤其是当你使用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错误的方式会让所有将来需要阅读你的测试的人感到困惑。如果您仍有疑问,请提出新问题 - 这次请对您尝试自动化的用例进行一些总结。

编辑:参见@setup这给了你一种方法来设置整个Examples以动态的方式:https://github.com/karatelabs/karate#setup

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将变量传递到示例部分[重复] 的相关文章

随机推荐