Is it possible to use karate 'match' inside conditional statement?

 ̄綄美尐妖づ 提交于 2020-07-13 00:21:56

问题


Find the example here.

def a = condition ? " karate match statement " : "karate match statement"

Is it possible to do something like this??


回答1:


This is not recommended practice for tests because tests should be deterministic.

The right thing to do is:

  • craft your request so that the response is 100% predictable. do not worry about code-duplication, this is sometimes necessary for tests
  • ignore the dynamic data if it is not relevant to the Scenario
  • use self-validation expressions or schema-validation expressions for specific parts of the JSON
  • use the if keyword and call a second feature file
  • in some cases karate.abort() can be used to conditionally skip / exit early

That said, if you really insist on doing this in the same flow, Karate allows you to do a limited match via JS. Here is an example:

* def foo = { hello: 'world' }
* def result = true ? karate.match(foo, { hello: '#string'}) : {}
* match result == { pass: true, message: null }
* eval if (result.pass) karate.log('*** passed')

The result of karate.match() will return a JSON in the form { pass: '#boolean', message: '#string' }

If none of the above options work - that means you are doing something really complicated, so write Java interop / code to handle this



来源:https://stackoverflow.com/questions/50348629/is-it-possible-to-use-karate-match-inside-conditional-statement

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