无法使用"=="和“包含"在同一行场景中使用空手道中的条件逻辑 [英] Cannot use "==" and "contains" in same line of scenario using conditional logic in Karate
问题描述
Lets says our implemented server v1 and v2 response looks as follows
v1Response = { id: "1", name: "awesome" }
v2Response = { id: "2", name: "awesome", value: "karate" }
类似地,我们为v1和v2定义客户端架构,如下所示
Similarly we define the client schema for v1 and v2 like as follows
v1Schema = { id: "#string", name: "#string }
v2Schema = { id: "#string", name: "#string, value: "#string" }
我们在通用方案中实现架构验证,如下所示.我们可以根据环境轻松地使用v1Response/v2Response设置响应",并使用v1Schema/v2Schema设置模式".
We implement schema validation in our generic scenario as follows. We can easily set the "response" with either v1Response/v2Response AND "schema" with either v1Schema/v2Schema depending on our the environment.
* match response == schema
只要我们针对v1客户端测试v1服务器/针对v2客户端测试v2服务器,上述通用脚本都可以正常工作.但是,例如,当我们要测试向后兼容性时,我们不能重用同一场景 针对v1客户端的v2服务器.在这种情况下
Above generic script works perfectly fine as long as we are testing v1 server against v1 client / v2 server against v2 client. However we cannot re-use the same scenario when we want to test backward compatibility for example v2 server against v1 client. In this case
* match response (actually v2Response) == schema (actually v1Schema) <--- will fail
因此,为了使其工作并进行向后兼容性测试,我还想使用空手道的包含"功能,例如
So in order to make it work and do backward compatibility testing, I also wanted to use karate "contains" feature like
* match response (actually v2Response) contains schema (actually v1Schema) <--- will pass
但是,为了使我的方案保持通用,目前无法执行任一操作
However in the quest to keep my scenarios generic it is currently not possible to do either
- 将两个==/包含在同一行脚本中,如下所示
- serverVersion == clientVersion? (匹配响应==模式):(匹配响应包含模式)
- Use both ==/contains in the same line of script like as follows
- serverVersion == clientVersion ? (match response == schema) : (match response contains schema)
或
-
使用一些标志,如下所示
Using some flag as follows
- 匹配响应SOMEFLAG模式
根据我们正在测试的环境,可以在karate-config.js中将SOMEFLAG设置为"=="或"contains".
whereas SOMEFLAG can be set to either "==" or "contains" in karate-config.js depending on environment we are testing.
编辑
在上面的示例中,我要做的只是测试以下应通过的情况
From the above example, all I want is to test following cases that should pass
* match v1Response == v1Schema
* match v2Response == v2Schema
* match v2Response contains v1Schema
使用如下通用行
* match response == schema <--- can it possibly be solved using above suggested solutions ?
推荐答案
由于某种原因,您认为入侵match
子句是解决此问题的唯一方法.请保持开放的态度,在这里,您要去:
For some reason you feel that hacking the match
clause is the only way to solve this problem. Please keep an open mind and, here you go:
* def schemas =
"""
{
v1: { id: "#string", name: "#string" },
v2: { id: "#string", name: "#string", value: "#string" }
}
"""
* def env = 'v1'
* def response = { id: "1", name: "awesome" }
* match response == schemas[env]
* def env = 'v2'
* def response = { id: "2", name: "awesome", value: "karate" }
* match response == schemas[env]
* def response = { id: "1", name: "awesome" }
* match response == karate.filterKeys(schemas[env], response)
最后一行是您所能获得的.
The last line is as generic as you can get.
这篇关于无法使用"=="和“包含"在同一行场景中使用空手道中的条件逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!