【问题标题】:Can we call a scenario from one feature in another using karate?我们可以使用空手道从一个功能中调用另一个场景吗?
【发布时间】:2017-09-22 19:43:30
【问题描述】:

我们有一个包含多个场景的功能 A。我们需要该文件中的一个场景。我们可以在功能 B 中调用它吗?

【问题讨论】:

  • 我对这种方法有一点担忧:空手道应该允许在特定场景下从功能文件本身重用。如果我有这么多的案例,那么我最终会得到很多功能文件,这些文件会保留重复的场景,只是为了被调用。

标签: karate


【解决方案1】:

没有。您需要将该场景提取到单独的*.feature 文件中,然后使用call 关键字重新使用它。

编辑:空手道 0.9.0 及以后的版本将支持按标签调用,如下所示:

* def result = call read('some.feature@tagname')

【讨论】:

  • 谢谢@Peter Thomas。我们真的很喜欢使用空手道。最初的想法是有一个关于身份验证的场景功能,其中还包括一个成功的场景。我们想将它用于其他需要它的功能(访问此成功场景返回的令牌)。那么,我们可以把这个成功的场景放在 JavaScript 中,并把我们的身份验证功能分开吗?你会推荐什么?如果是这样,您能否给我们一些关于如何在 JS 中执行此场景的提示?
  • 请查看调用和标头的演示示例。然后提出具体问题。
【解决方案2】:

用一个例子来说明answer from Karate-inventor Peter Thomas

给定一个特征文件some.feature,其中有多个场景标记tag-decorator

@tagname
Scenario: A, base case that shares results to e.g. B
 // given, when, then ... and share result in a map with keyword `uri`
 * def uri = responseHeaders['Location'][0]

@anotherTag
Scenario: X, base case that shares results to e.g. B
 // given, when, then ... and share result in a map with keyword `createdResourceId`
 * def createdResourceId = $.id

在另一个特性中,我们可以通过它的标签从那个特性中调用一个特定的 scenario,例如tagname:

Scenario: B, another case reusing some results of A
 * def result = call read('some.feature@tagname')
 * print "given result of A is: $result"
 Given path result.uri + '/update'

另见:demo of adding custom tags to scenarios

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多