【问题标题】:Karate - Ability to dynamically decide the type of match in karate for verification空手道 - 能够动态决定空手道比赛的类型以进行验证
【发布时间】:2019-12-02 20:40:25
【问题描述】:

假设我们为不断发展的服务器编写了以下场景的脚本

Actual server v1 response 
response = { id: "1", name: "karate" }
Mocking client v1 schema 
schema = { id: "#string", name: "#string }
* match response == schema

Actual server v2 response
response = { id: "2", name: "karate", value: "is" }
Mocking client v2 schema
schema = { id: "#string", name: "#string, value: "#string" }
* match response == schema

Actual server v3 response
response = { id: "3", name: "karate", value: "is", description: "easy" }
Mocking client v3 schema
schema = { id: "#string", name: "#string", value: "#string", description: "#string" }
* match response == schema

类似地,为了对我们不断发展的服务器进行向后兼容性测试,我们按照以下方式编写场景脚本

Actual server v3 response
response = { id: "3", name: "karate", value: "is", description: "easy" }
Mocking client v1 schema 
schema = { id: "#string", name: "#string }
* match response contains schema

Actual server v2 response
response = { id: "2", name: "karate", value: "is" }
Mocking client v1 schema 
schema = { id: "#string", name: "#string }
* match response contains schema

Actual server v1 response 
response = { id: "1", name: "karate" }
Mocking client v1 schema 
schema = { id: "#string", name: "#string }
* match response contains schema

提议是能够在匹配语句中使用某种标志,动态决定我们在测试期间进行的匹配类型。 假设标志的名称是 SOMEFLAG,我们提供了我们想要在测试期间进行的匹配类型(在 karate-config.js 中设置以实现全局效果)

var SOMEFLAG = "contains"; 
OR
var SOMEFLAG = "==";

现在在场景中我们执行以下操作

# Depending on what is set in karate-config.js, it will use either contains or == for verification.
* match response SOMEFLAG schema

在空手道中可以做到这一点吗?

还要注意,这个想法的成功实际上取决于https://github.com/intuit/karate/issues/826,因为使用包含匹配的能力匹配嵌套对象。

【问题讨论】:

    标签: dsl karate


    【解决方案1】:

    就我个人而言,我强烈反对这个想法,因为它会降低您的测试的可读性。一旦开始,这是一个滑坡。例如,当您尝试过多重用时会发生什么(是的,重用在测试自动化中可能是一件坏事,我真的不在乎您是否不同意 :) - 请参阅:https://stackoverflow.com/a/54126724/143475

    我会做这样的事情:

    * def lookup = 
    """
    {
      dev: { id: "#string", name: "#string },
      stage: { id: "#string", name: "#string, value: "#string" },
      preprod: { id: "#string", name: "#string", value: "#string", description: "#string" }
    }
    """
    * def expected = lookup[karate.env]
    * match response == expected
    

    编辑 - 我觉得我们在这次讨论之后所做的改变也将解决你的问题 - 或者至少给你一些新的想法:https://github.com/intuit/karate/issues/810

    【讨论】:

    • 采取向后兼容性示例,我们将 v3 服务器响应与上述示例中的 v1 客户端模式匹配您的第一个解决方案不起作用,因为 * def lookup = v1ClientSchema: { id: "#string", name: "# string } * def expected = lookup[karate.env] * match response == expected 您的第二个解决方案查看 karate.filterKeys(),SCHEMA 需要是超集,但在我的情况下,我的响应充当超集。所以这个也不行。我会说如果空手道提供了一种动态决定方法、响应代码的方法,那么为什么不匹配语句呢?
    • @Dixie contains 如果您的响应是超集,则将始终有效,所以我不明白。如果需要,请将此答案标记为已接受并打开一个新问题,并通过一个清晰的示例保持简单。如果您不满意,请务必提供修复 - 这是我们正在谈论的开源。
    • 我标记了你的答案,但是由于我的声誉低于 15,我猜它没有公开显示。在旁注中,我认为我最初的问题已经足够清楚了。
    • @Dixie 不,因为我无法阅读您上面的评论,请也阅读此内容 - 如果您需要,请再试一次:stackoverflow.com/help/how-to-ask
    猜你喜欢
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多