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

时间:2019-07-24 16:11:16

标签: dsl karate

让我们说,我们为不断发展的服务器编写了以下脚本脚本

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

建议是能够在match语句中使用某种标志,该标志可以动态地决定我们在测试过程中进行的匹配的类型。 可以说,标志的名称是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

1 个答案:

答案 0 :(得分:0)

我个人强烈反对这个想法,因为它会使您的测试不那么可读。一旦开始,这是一个湿滑的斜坡。举一个例子,说明当您尝试过多地重复使用时(是的,在测试自动化中重复使用可能是一件坏事,如果您不同意,我真的不在乎:)-参见: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