Book Image

Writing API Tests with Karate

By : Benjamin Bischoff
Book Image

Writing API Tests with Karate

By: Benjamin Bischoff

Overview of this book

Software in recent years is moving away from centralized systems and monoliths to smaller, scalable components that communicate with each other through APIs. Testing these communication interfaces is becoming increasingly important to ensure the security, performance, and extensibility of the software. A powerful tool to achieve safe and robust applications is Karate, an easy-to-use, and powerful software testing framework. In this book, you’ll work with different modules of karate to get tailored solutions for modern test challenges. You’ll be exploring interface testing, UI testing as well as performance testing. By the end of this book, you’ll be able to use the Karate framework in your software development lifecycle to make your APIs and applications robust and trustworthy.
Table of Contents (15 chapters)
1
Part 1:Karate Basics
7
Part 2:Advanced Karate Functionalities

Matching status codes and responses

In the last output, we saw that we get a status code of 200 (also known as OK). This means the request was successful. Let’s add a step to verify this.

Matching the status code and type

If you want to match a simple status code, this is done via the status keyword like this:

Scenario: Checking user specific posts
    Given url 'https://jsonplaceholder.typicode.com/posts'
    When method get
    Then status 200

We use the Then keyword here to indicate that this is an assertion. At this point, we can run the test (which should pass).

Status codes

For a complete list of HTTP status codes, see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status.

It is a good idea to deliberately make it fail once to verify that this test would not give us a false positive result in an error case.

Making the test fail

Let’s change the status code to the...