空手道 - 能够动态决定空手道比赛类型以进行验证

Karate - Ability to dynamically decide the type of match in karate for verification

假设我们按照我们不断发展的服务器的方式编写了场景脚本

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 由于能力匹配嵌套对象使用包含匹配。

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

我会做的是这样的:

* 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