Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

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

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?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...