error-handlingkarate

how to handle error mechanisms in testing API calls using Karate test framework


Is there a good way to handle connection refused, 500 response error while testing REST calls using karate test framework? I have a feature file with 10 scenarios to test a REST API call with different data. If connection to rest api itself fails or I get 500 error response, I don't even want to execute scenarios in feature file. Also is there a way I can display a report or message with those error messages

Here is my karate-con gif.js file:

function() {

var config = {
url: 'http://localhost:8080/api/test',
timeout: 5000
};

try {
var response = karate.callSingle('classpath:API.feature');

    if (response.responseCode != 200) {
      throw new Error('API call failed');
    }

} catch(e) {
config.errorMessage = e.message;
karate.log('Error: ' + e.message);
}

return config;
}

Here is my feature file:

Feature: Testing valid message transmission via REST API

Background:
\* url url
\* configure retry = { count: 3, interval: 5000 }

Scenario Outline: Transmit a valid message with various version numbers via POST method successfully and capture the success response
Given request (“abcdef <version>”)
When method POST
Then status 200
And print response
And print version
And def responseMessage = response

    Examples:
      | version|
      | 2.1    |
      | 2.2    |
      | 2.3    |
      | 2.4    |
      | 2.5    |
      | 2.6    |

Scenario: Transmit an empty message via POST method successfully and capture the error response
Given request ''
When method POST
Then status 400
And match response contains { error: 'Bad Request' }
And def errorMessage = response.error
And print errorMessage\`

Solution

  • Two suggestions: