Data-driven testing is a methodology where test data is separated from test scripts, enabling the execution of a test case with different sets of input values. This approach increases test coverage, reduces maintenance costs, and improves the scalability of tests. Combining Cucumber, a popular behavior-driven development (BDD) framework, with DynamoDB, AWS’s NoSQL database service, offers a robust solution for managing and executing data-driven tests in a scalable and efficient manner.

Why Data-Driven Testing?

Data-driven testing provides several benefits:

  1. Separation of Test Logic and Test Data: Keeping test data separate from test scripts makes it easier to maintain and update tests.
  2. Reusability: Test scripts can be reused with different sets of data, reducing redundancy.
  3. Scalability: Easily scale your tests to cover various input scenarios without modifying the test logic.
  4. Enhanced Test Coverage: By using diverse data sets, you can ensure broader test coverage, capturing edge cases and potential bugs.

Introduction to Cucumber and DynamoDB

Cucumber

Cucumber is a BDD framework that allows writing tests in a human-readable format using Gherkin language. This facilitates collaboration among developers, testers, and non-technical stakeholders. Cucumber tests are written in plain English and are mapped to test scripts written in programming languages such as Java, JavaScript, or Python.

DynamoDB

DynamoDB is a fully managed NoSQL database service provided by AWS. It offers high scalability, low latency, and flexible schema design. DynamoDB is suitable for applications requiring a database that can handle large volumes of data and high request rates.

Setting Up Cucumber with DynamoDB

Let’s walk through the process of setting up a data-driven testing environment using Cucumber and DynamoDB.

Prerequisites

  1. AWS Account: Ensure you have an AWS account with access to DynamoDB.
  2. Java Development Kit (JDK): Install JDK on your machine.
  3. Maven: Use Maven for managing dependencies and building the project.
  4. Cucumber: Add Cucumber dependencies to your project.

Project Structure

A typical Maven project structure for Cucumber tests looks like this:

src
 └── main
     └── java
 └── test
     └── java
         └── com
             └── example
                 ├── runner
                 ├── stepdefs
                 └── util
 └── resources
     └── features
pom.xml

Adding Dependencies

Add the following dependencies to your pom.xml:

<dependencies>
    <!-- Cucumber dependencies -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>7.0.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>7.0.0</version>
        <scope>test</scope>
    </dependency>
    
    <!-- AWS SDK for DynamoDB -->
    <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>dynamodb</artifactId>
        <version>2.17.28</version>
    </dependency>
    
    <!-- Other dependencies -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.7.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Configuring DynamoDB

First, create a DynamoDB table to store test data. You can use the AWS Management Console or AWS CLI to create the table. For this example, we’ll create a table named TestData with a primary key TestId.

aws dynamodb create-table --table-name TestData \
  --attribute-definitions AttributeName=TestId,AttributeType=S \
  --key-schema AttributeName=TestId,KeyType=HASH \
  --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5

Next, populate the table with test data. Here’s an example JSON file (test-data.json):

[
  {
    "TestId": "1",
    "Username": "user1",
    "Password": "pass1"
  },
  {
    "TestId": "2",
    "Username": "user2",
    "Password": "pass2"
  }
]

Load the data into DynamoDB using AWS CLI:

aws dynamodb batch-write-item --request-items file://test-data.json

Writing Cucumber Feature Files

Create a feature file in the src/test/resources/features directory. This file defines the test scenarios in Gherkin language. Here’s an example feature file (login.feature):

Feature: User Login

  Scenario Outline: Successful login
    Given a user with username "<Username>" and password "<Password>"
    When the user attempts to log in
    Then the login should be successful

  Examples: 
    | TestId |
    | 1      |
    | 2      |

Implementing Step Definitions

Create step definition classes in the src/test/java/com/example/stepdefs directory. These classes contain the code that executes the steps defined in the feature files. Here’s an example step definition (LoginStepDefinitions.java):

package com.example.stepdefs;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.GetItemRequest;
import software.amazon.awssdk.services.dynamodb.model.GetItemResponse;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class LoginStepDefinitions {
    private String username;
    private String password;

    private DynamoDbClient dynamoDbClient = DynamoDbClient.create();

    @Given("a user with username {string} and password {string}")
    public void givenAUserWithUsernameAndPassword(String testId) {
        GetItemRequest request = GetItemRequest.builder()
                .tableName("TestData")
                .key(Map.of("TestId", AttributeValue.builder().s(testId).build()))
                .build();
        
        GetItemResponse response = dynamoDbClient.getItem(request);
        Map<String, AttributeValue> item = response.item();

        this.username = item.get("Username").s();
        this.password = item.get("Password").s();
    }

    @When("the user attempts to log in")
    public void whenTheUserAttemptsToLogin() {
        // Simulate login attempt
    }

    @Then("the login should be successful")
    public void thenTheLoginShouldBeSuccessful() {
        // Simulate login success check
        assertTrue(true);
    }
}

Running the Tests

Create a test runner class in the src/test/java/com/example/runner directory. This class uses JUnit to run the Cucumber tests. Here’s an example test runner (TestRunner.java):

package com.example.runner;

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/resources/features",
        glue = "com.example.stepdefs"
)
public class TestRunner {
}

Conclusion

By integrating Cucumber with DynamoDB, you can effectively manage and execute data-driven tests. DynamoDB provides a scalable and flexible solution for storing test data, while Cucumber’s BDD approach makes tests easy to understand and maintain. This combination is powerful for developing robust and scalable test automation frameworks.

This setup not only enhances test coverage but also simplifies the management of test data, making your testing process more efficient and reliable.

If you found this article helpful and are interested in integrating Cucumber Automation Framework with AWS DynamoDB, I suggest you check out some of the other articles I’ve written for this series:

If you’re looking for a deeper dive into some of the concepts and specifics discussed in my article, feel free to reach out to me directly or as always you can checkout the official AWS DynamoDB Developer Documentation for more information.

Related Posts