Can I use the status shortcut in Karate to check a response class instead of just one code

帅比萌擦擦* 提交于 2021-02-08 07:41:05

问题


I am using Karate to test other people's implementations of an API. When checking response status codes I often need to accept more than one response. For example in response to a PUT I may see 200, 201, 205 - they are equally valid. I know I can use

Then assert responseStatus >= 200 && responseStatus < 300

to check for success but the shortcut really helps readability of the tests.

Would you consider an enhancement to the language to support response classes such as:

  • success (meaning 200-299)
  • redirect (meaning 300-399)
  • fail (meaning 400-499)
  • 2xx
  • 3xx
  • 4xx

If I were to look at submitting a PR for this would you agree it is useful and would you have a preferred mechanism? Would these classes be best as parsed symbols or strings that force a different match to be implemented when it detects the status is not a number?


回答1:


Yes, my first reaction is not to add a new keyword. Also to be honest, this seems to be a rare requirement - never had this ask before, I guess API testing would generally mean predictable responses.

My proposal is that you can write a custom function:

* def statusSuccess = function(){ var status = karate.get('responseStatus'); return status >= 200 && status < 300 }

* url 'https://httpbin.org'
* path 'status', 200
* method get
* assert statusSuccess()


来源:https://stackoverflow.com/questions/65843167/can-i-use-the-status-shortcut-in-karate-to-check-a-response-class-instead-of-jus

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