1

I want to assert responseTime of all scenarios. But i do not want to repeat the assertion code in every scenario. Below is my feature file:

Feature: Reqres api test cases

  Background: base url
    Given url base_url

  Scenario: list single user get request
    Given path single_user_path
    When method get
    Then status 200
    And assert responseTime < 4000

Scenario: create user using post and inline json payload
    * def path = '/users'
    Given path path
    And request {"name": "morpheus","job": "leader"}
    When method post
    Then status 201
    And assert responseTime < 4000

In the above code block, I want to avoid responseTime assertion duplication. How to achieve this in karate?. Please help.

cyrilgeorge153
  • 331
  • 1
  • 2
  • 9

1 Answers1

1

No this is not supported and not planned either. It is unlikely every API call will have the exact same SLA. Also this is what the Gatling integration is for: https://stackoverflow.com/a/55146463/143475

EDIT as an example of how you can do "reuse" of response assertions:

Feature:

Background:
* def validateResponse =
"""
function() {
  var contentType = karate.get("responseHeaders['Content-Type'][0]");
  if (contentType !== 'application/json') {
    karate.fail('content type is not json');
  }
  var responseType = karate.get('responseType');
  if (responseType !== 'json') {
    karate.fail('response type is not json');
  }
}
"""

Scenario:
* url 'https://httpbin.org/post'
* request { foo: 'bar' }
* method post
* validateResponse()

Please note that I absolutely don't recommend the above approach because of reasons best explained here: https://stackoverflow.com/a/54126724/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • And match header Content-Type == 'application/json; charset=utf-8' And match responseType == 'json' . I do have these two assertions as well in my framework which is common to all the scenarios. So there is now way to reuse this assertion rather than repeating these in every scenarios right? I was expecting a feature like response specification similar to Rest-assured. So there is no plan to implement this right? @PeterThomas – cyrilgeorge153 Apr 14 '22 at 06:17
  • @cyrilgeorge153 may I humbly offer my opinion that those 2 assertions are a waste of time when it comes to API testing. that said there are many options such as you can re-use a common function, e.g. https://stackoverflow.com/a/62911366/143475 - as long as you are ok to write one extra line after each scenario (see my edit to the answer) if that is too much work, kindly look for another framework and yes there is no plan to implement this unless you want to contribute ;) – Peter Thomas Apr 14 '22 at 07:15
  • 1
    I was just confirming. Thanks for the clarification and suggesting other way. I have used other frameworks in the past and changed to Karate due to its simplicity. No plan to shift back. – cyrilgeorge153 Apr 14 '22 at 07:28
  • @cyrilgeorge153 glad to hear that. I tend to get defensive because there are too many test-automation engineers out there I sometimes meet - who expect everything to work like REST-assured and Java and are being forced by their managers to use Karate. I created Karate very deliberately to be different. thank you for understanding. – Peter Thomas Apr 14 '22 at 07:31