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

让人想犯罪 __ 提交于 2019-12-11 04:59:10

问题


Lets say we scripted the scenarios following way for our evolving servers

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

Similarly for backward compatibility testing of our evolving servers, we script the scenarios following way

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

Proposal is to be able to use some kind of flag in match statement that dynamically decides the kind of match we do during testing. Lets say that the name of flag is SOMEFLAG and we provide the kind of match we want to do during testing (set in karate-config.js for global effect)

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

Now in scenario we do following

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

Is it possible to do this in karate ?

Also note that success of this idea really depends on https://github.com/intuit/karate/issues/826 due to ability match nested object using contains match.


回答1:


Personally, I am strongly against this idea because it will make your tests less readable. It is a slippery slope once you start this. For an example of what happens when you attempt too much re-use (yes, re-use can be a bad thing in test automation, and I really don't care if you disagree :) - see this: https://stackoverflow.com/a/54126724/143475

What I would do is something like this:

* 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

EDIT - I have a feeling that the change we made after this discussion will also solve your problem - or at least give you some new ideas: https://github.com/intuit/karate/issues/810



来源:https://stackoverflow.com/questions/57187219/karate-ability-to-dynamically-decide-the-type-of-match-in-karate-for-verificat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!