Core Java

Cucumber: Pass List Param

Cucumber is a popular tool used for Behavior-Driven Development (BDD). When writing tests, passing different types of data to step definitions is often necessary. Let us delve into understanding how to pass a list param in a Cucumber testing scenario using various techniques.

1. Introduction to Cucumber

Cucumber is an open-source tool designed to support Behavior-Driven Development (BDD). BDD is a software development process that emphasizes collaboration between developers, testers, and business stakeholders. Cucumber allows writing tests in a human-readable format using a plain-text language called Gherkin, which non-technical team members can easily understand.

1.1 Benefits of Cucumber

  • Collaboration: Cucumber promotes close collaboration between developers, testers, and business stakeholders by allowing all parties to participate in defining test cases.
  • Human-Readable Tests: Tests are written in Gherkin, a plain-text language that is easy to read and understand for non-programmers. This bridges the gap between technical and non-technical team members.
  • Executable Specifications: Gherkin feature files serve as both documentation and automated tests. This ensures that the documentation stays up to date with the actual functionality of the system.
  • Supports Multiple Languages: Cucumber supports a wide range of programming languages including Java, Ruby, JavaScript, and more, making it versatile for various tech stacks.
  • BDD Adoption: By following BDD principles, Cucumber encourages developing software that meets the exact needs of stakeholders and minimizes misunderstandings.

1.2 Advantages of Cucumber

  • Improves Communication: Cucumber enables clear communication between stakeholders and developers, as the tests are written in a natural language style that everyone can understand.
  • Ensures Feature Completeness: Since Cucumber scenarios are written from the perspective of the end-user, they ensure that the software meets the expected behavior as defined by business stakeholders.
  • Easy to Maintain: Gherkin scenarios, being readable and simple, are easy to maintain and modify as the software evolves.
  • Integration with Other Tools: Cucumber integrates well with other tools such as Selenium for UI testing or Rest-Assured for API testing, enhancing its testing capabilities.
  • Reusable Step Definitions: Cucumber allows for reusing step definitions across different scenarios, reducing redundancy and improving efficiency.

1.3 Disadvantages of Cucumber

  • Requires Learning Gherkin: Although Gherkin is simple, there is still a learning curve for team members unfamiliar with it. Writing scenarios correctly may take practice.
  • Time-Consuming to Write Tests: Writing Gherkin scenarios and step definitions can be more time-consuming compared to traditional unit testing, especially for complex scenarios.
  • Maintenance Overhead: As the number of scenarios grows, maintaining them can become challenging, particularly if the tests are not organized properly.
  • Not Suitable for All Types of Testing: Cucumber is primarily designed for functional and acceptance testing. It may not be the best choice for lower-level testing like unit testing or performance testing.
  • Overhead for Small Projects: For smaller projects, the benefits of Cucumber’s collaborative and BDD approach may not outweigh the overhead of writing Gherkin scenarios and maintaining the test suite.

2. Understanding Cucumber Parameters

Cucumber allows you to define steps in a human-readable format (usually written in Gherkin). To connect these steps with actual code, parameters can be passed. A parameter can be a string, number, or even a list, and the Cucumber framework provides flexible ways to capture these values in your step definitions.

For example, a step might look like this:

Given I have the following items: ["Apple", "Banana", "Orange"]

In this case, we need to capture the list of items (“Apple”, “Banana”, “Orange”) and pass it to the step definition as a list. Let’s explore different ways of achieving this.

2.1 Using DataTable for List Parameters

The DataTable object in Cucumber is used to represent tabular data. It can also be used to capture lists when working with multiple values.

2.1.1 Step Definition Example

Given the following fruits:
| Apple  |
| Banana |
| Orange |

2.1.2 Step Definition Code:

@Given("the following fruits:")
public void the_following_fruits(DataTable dataTable) {
  List < String > fruits = dataTable.asList(String.class);
  System.out.println("Fruits: " + fruits);
}

The code defines a:

  • @Given: This is the Gherkin step-matching annotation. It matches the step “Given the following fruits”.
  • DataTable: The DataTable parameter captures the table from the feature file defined in Section 2.1.1.
  • asList: The method asList converts the DataTable into a list of Strings.

The code gives the following output:

Fruits: [Apple, Banana, Orange]

2.2. Using Regular Expressions

Cucumber also allows regular expressions to capture parameters in a step. This is useful when the list format is more compact, such as a comma-separated list.

2.2.1 Step Definition Example

Given I have the following items: Apple, Banana, Orange

2.2.2 Step Definition Code:

@Given("^I have the following items: (.*)$")
public void i_have_the_following_items(String items) {
  List < String > itemList = Arrays.asList(items.split(", "));
  System.out.println("Items: " + itemList);
}

The code defines a:

  • Regular Expression: The (.*) part captures all characters after the step phrase “I have the following items:”.
  • split: The split method is used to split the string by commas and spaces into individual list elements.
  • Arrays.asList: This converts the array of items into a list.

The code gives the following output:

Items: [Apple, Banana, Orange]

2.3 Using Custom Transformer

If you have a more complex input format or need specific transformations, you can create a custom transformer using Cucumber’s parameter type mechanism.

2.3.1 Define a Custom Parameter Type

In your step definition class, define a custom parameter type using the @ParameterType annotation:

@ParameterType(".*")
public List < String > listOfItems(String items) {
  return Arrays.asList(items.split(", "));
}

2.3.2 Usage in Step Definition

Now you can directly use this custom parameter in your step definitions:

@Given("I have the following items: {listOfItems}")
public void i_have_items(List < String > items) {
  System.out.println("Items: " + items);
}

The code defines a:

  • @ParameterType: This annotation defines a custom parameter type named listOfItems. The pattern .* captures all input and passes it to the method.
  • listOfItems: The method splits the input string into a list, just as before.
  • {listOfItems}: The step uses the custom parameter type in the Gherkin step definition.

The code gives the following output:

Items: [Apple, Banana, Orange]

3. Conclusion

In Cucumber, passing a list as a parameter can be achieved in multiple ways depending on the format of the input and the complexity of the data. Using DataTable is a straightforward method for tabular data, while regular expressions can handle comma-separated values. Custom transformers provide additional flexibility, especially for more complex input formats. Each approach allows you to capture and transform input effectively for BDD-style testing.

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button