Search:

Automated API Testing is Easier with Karate

Automated API Testing is Easier with Karate - kloia Blog

Behaviour-Driven Development (BDD) is a software development approach that provides communication between technical and non-technical teams. It is evolved from Test Driven Development and similar with TDD, but BDD mostly deals with the user behavior of the system.

Karate Framework is an open source tool for API test automation. Tests are written in Gherkin format which is also using for BDD. So even non-developers can be able to create API tests for services. Let’s check some of the main features of the framework and how API test development became easier with it.

scenario

1-API Request Methods

As you know, there are different API request methods such as "Get", "Put", "Update" etc. Calling an endpoint using with API request method is very simple by Karate as below. Just type request method which you need after "method" keyword.

When method GET


When method POST


When method DELETE


When method PATCH
...

2-Assertion and Validation

Assertions and Validations are the key points of the test processes to check responses or expected results. It's not possible to make sure that the application without assertions or validations of test cases is working correctly.

At this point, Karate emerges a variety of solutions to verify test cases.

  • Status Code Check
Then status 200
  • Response Value Check
And match product.data[0].name == 'We are KLOIANs'
  • Response Contains Key-Value Check
And match response contains {price: 2.5}

 

3-Reading Files

Reading files is a common need in test automation project to re-use or manipulate payload data, scripts or functions. Especially I’ve faced a need to read and modify DTOs many times in the projects. When focused on reading files, Karate supports to read .json, .xml, .yaml, .js, .csv or .txt data file type.

* def json = read('some.json')
* set json.item[] = 'string'

And request read('product-update-data.json')

4-Calling Feature File or Scenario

Developing same test cases and codes over and over again is a problem for development . To avoid writing repetitive scenarios or steps, use “call” method. “call” method can be used for whole feature file or a specific scenario by its tag.

Given call read('call.feature')
...

Given call read('classpath:call.feature@tag')
...

     CONCLUSION

     For the past few years, software testing has become important and resources allocated for testing increased. With the widespread of micro-service architecture, API testing has taken an important place in software testing.

     Testing API manually is inadequate anymore. Therefore, we should support the manual process with automated tests, increase the quality and reliability of application. Karate comes up with quite simple framework to develop automated API test cases. You even don't have to develop any additional codes mostly, because it's very comprehensive and powerful library!

Oğulcan Toker

Junior Test Automation Consultant at kloia

Want to work with us?

A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in this spot.

Contact us