API testing using Cucumber

What is an API?

APIs are mechanisms that enable two software components to communicate with each other using a set of definitions and protocols. API stands for Application Programming Interface. APIs work by sharing data between applications, systems, and devices. This happens through a request and response cycle. A user initiates a request for data by interacting with an application. The request is sent to the API, which retrieves the data and returns it to the user.

REST is the most popular API architecture for transferring data over the internet. In a RESTful context, resources are accessible via endpoints, and operations are performed on those resources with standard HTTP methods such as GET, POST, PUT, and DELETE.

In order to better understand this process, it can be useful to think of APIs like restaurants. In this metaphor, the customer is like the user, who tells the waiter what she wants. The waiter is like an API, receiving the customer's order and translating it into easy-to-follow instructions for the kitchen—sometimes using specific codes or abbreviations that the kitchen staff will recognize. The kitchen staff is like the API server because it creates the order according to the customer's specifications and gives it to the waiter, who then delivers it to the customer.

What is API testing?

API testing is the process of confirming that an API is working as expected. Developers can run API tests manually, or they can automate them. Traditionally, API testing has occurred at the end of the development phase, but an increasing number of teams are running tests earlier in the API lifecycle. This approach to API testing, which is known as "shifting left," supports rapid iteration by enabling teams to catch and fix issues as soon as they are introduced.

What is Cucumber? 

Cucumber is a popular tool for Behavior-Driven Development (BDD). It allows teams to describe application behavior in plain, human-readable language, making it a powerful tool for collaboration between developers, testers, and non-technical stakeholders. 

Gherkin Language 

Cucumber uses the Gherkin language to write test scenarios. Gherkin is easy to read and write, and it follows a structured format known as Given-When-Then, which describes the context, actions, and expected outcomes of a test. 

Setting Up the Environment 

To get started with Cucumber for REST API testing, you'll need to set up your development environment. 

Prerequisites 

Before you begin, make sure you have the following in place: 

  • A development environment with your preferred programming language (e.g., Java, Python, JavaScript) installed. 
  • Cucumber and any necessary libraries or dependencies installed. 

Configuration 

Configure Cucumber and your chosen programming language to work together seamlessly. This typically involves adding dependencies to your project and configuring test runners. 

Writing Feature Files 

Cucumber tests start with feature files, which describe the behavior of your application. Feature files are written in Gherkin and follow a structured format. 

Structure of Feature Files 

A typical feature file consists of the following sections: 

  • Feature: Describes the overall feature being tested. 
  • Scenario: Represents a specific test scenario. 
  • Given: Describes the initial Condition. 
  • When: Specified condition 
  • Then: Defines the expected outcome. 

Here's an example feature file for testing a REST API: 

Feature: API Testing
  Scenario: Verify GET request
    Given the API endpoint "/api/resource"
    When I send GET request
    Then the response status code is should be 200
    And the response body should contain "Expected content"

Creating Step Definitions 

Step definitions bridge the gap between feature files and code. They map Gherkin steps to actual actions performed by your test automation code. 

Role of Step Definitions 

Step definitions: 

  • Interpret Gherkin steps. 
  • Execute actions like making API requests. 
  • Perform assertions to validate API responses. 

Here's an example of step definitions in Java for the scenario mentioned earlier: 

@Given("the API endpoint {string}")
public void the_api_endpoint(String endpoint) {
    // Code to set the API endpoint
}

@When("I send a GET request")
public void i_send_a_get_request() {
    // Code to send a GET request
}

@Then("the response status code should be {int}")
public void the_response_status_code_should_be(int statusCode) {
    // Code to check the response status code
}

@Then("the response body should contain {string}")
public void the_response_should_contain(String expectedContent) {
    // Code to check if response body contains expected content
}

Conclusion 

Using Cucumber for REST API testing provides a structured and collaborative approach to ensure the reliability of your API-driven applications. By writing human-readable feature files and mapping them to step definitions, you create tests that are easy to understand and maintain. 

API testing is a critical part of the software development process, and with Cucumber, you can make it more efficient and effective. Start by setting up your environment, writing feature files, creating step definitions, and integrating with a test automation framework. As you gain experience, you can apply best practices to handle authentication, manage test data, and overcome common challenges. 

By harnessing the power of Cucumber, you'll be well-equipped to deliver high-quality APIs and ensure the seamless interaction of different software components. 

If your team is struggling with your Test Automation strategy, the Test Orchestrators at Testery can help.