How to use Serenity with Cucumber
Ensuring the quality and functionality of your applications is crucial in the ever-changing field of software development. Cucumber is a particularly noteworthy tool in the automated testing space because of its test scenarios based on natural language. It elevates test automation to a new level when combined with the Serenity BDD framework. This blog post will examine Cucumber with Serenity’s features and how it might improve your test automation endeavors. This guide can help you enhance your testing approach with Serenity or if you’re new to Cucumber.
Table of Contents
- What is Cucumber?
- An Introduction to Serenity BDD
- Combining Cucumber and Serenity
- Writing Feature Files
- Implementing Step Definitions
- Generating Rich Reports with Serenity
- Best Practices
- Final thoughts on combining Cucumber and Serenity
What is Cucumber?

For behavior-driven development (BDD), one well-liked open-source tool is Cucumber. Using scenarios written in simple language it enables collaboration on the testing process amongst non-technical stakeholders, developers, and testers. Because these scenarios are written in Gherkin syntax, team members with and without technical backgrounds can understand them with ease.
Cucumber encourages the organized description of an application’s behavior through the use of Given-When-Then phases. It facilitates improved teamwork and communication by serving as a link between technical implementation and test cases that can be read by humans.
An Introduction to Serenity BDD
An open-source framework called Serenity BDD, formerly known as Thucydides is used to create automated acceptance tests that are stronger and easier to maintain. It is compatible with Cucumber and easily interfaces with well-known testing libraries like JUnit and TestNG. Serenity is concentrated on producing comprehensive, dynamic documentation and comprehensible reporting for stakeholders.
The ability of Serenity to produce comprehensive, interactive reports that highlight problems, test results, and even user stories that are covered by your tests is one of the main benefits of adopting it. Being able to see the test execution process holistically makes it a very useful tool for test management.
Combining Cucumber and Serenity

The combination of Cucumber with Serenity results in a testing framework that combines Serenity’s reporting and live documentation features with the expressive power of Cucumber scenarios. For teams trying to increase test communication and produce thorough, accessible reports, this combination is revolutionary.
You must add the Serenity-Cucumber module to your project’s dependencies in order to integrate Cucumber with Serenity. Because of this, Serenity can now identify Cucumber scenarios and step definitions, which facilitates the creation of thorough test reports.
Writing Feature Files

Cucumber uses Gherkin syntax to define test scenarios in feature files. These feature files define the expected behavior of your application in a readable manner and act as living documentation. When combined with the power of Serenity, these feature files become the mainstay of your test suite, assisting you in keeping an organized and thorough record of your tests.
Example feature file:
Feature: Search by keyword
Scenario: Searching for a term
Given Serena is researching things on the internet
When she looks up "Cucumber"
Then she should see information about "Cucumber"
Implementing Step Definitions
You will need to write corresponding step definitions in code to run the scenarios described in your feature files. These step definitions convert your feature files’ natural language steps into executable code.
Example step definition in Java
package <your project package>;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import net.thucydides.core.annotations.Steps;
import starter.navigation.NavigateTo;
import starter.search.SearchFor;
import starter.search.SearchResult;
import static org.assertj.core.api.Assertions.assertThat;
public class SearchStepDefinitions {
@Steps
NavigateTo navigateTo;
@Steps
SearchFor searchFor;
@Steps
SearchResult searchResult;
@Given("^(?:.*) is researching things on the internet")
public void iAmOnTheWikipediaHomePage() {
navigateTo.theHomePage();
}
@When("she/he looks up {string}")
public void iSearchFor(String term) {
searchFor.term(term);
}
@Then("she/he should see information about {string}")
public void allTheTesultTitlesShouldContainTheWord(String term) {
assertThat(searchResult.displayed()).contains(term);
}
}
Example Runner file in Java
package <Your project Package>;
import io.cucumber.junit.CucumberOptions;
import net.serenitybdd.cucumber.CucumberWithSerenity;
import org.junit.runner.RunWith;
@RunWith(CucumberWithSerenity.class)
@CucumberOptions(
plugin = {"pretty"},
features = "src/test/resources/features"
)
public class CucumberTestSuite {}
Code Reference: Visit here for the starter project.
Generating Rich Reports with Serenity

Using Serenity with Cucumber has many advantages, one of which is the ability to create interactive, thorough reports. You can examine comprehensive HTML reports that offer a multitude of details regarding the test execution after your tests have been run. These reports include:
- Steps and scenarios’ pass/fail status.
- Duration of the test.
- Images and videos of test runs on computers.
- Comprehensive details about the test data and tags.
- Integration with popular CI/CD tools.
Sharing test results with your team and other stakeholders can be facilitated greatly by using these reports.
Best Practices

Think about implementing the following best practices to get the most out of Cucumber with Serenity:
- Each feature file should focus on a single user story or functionality.
- Write clear and eloquent Gherkin scenarios.
- Keep your step definitions organized and neat.
- Make sure your test reports are accurate and comprehensive by regularly reviewing and updating them.
- Incorporate your Cucumber-Serenity tests into your continuous integration/continuous development pipeline.
Final thoughts on combining Cucumber and Serenity
Cucumber works well when combined with the Serenity BDD framework to produce comprehensive, live documentation and expressive, readable test scenarios. Using this testing strategy, you can help your team members communicate and work together more effectively while also producing reports useful for all parties involved in understanding the application’s current state.
Remember to follow best practices and take full advantage of the reporting features as you investigate and use Cucumber with Serenity. You can confidently deliver high-quality software when you have these two in your testing toolkit.
In this blog post, we have discussed the foundations of Cucumber and Serenity, their integration, creating feature files and step definitions, generating reports, and best practices. It’s now your chance to use this compelling testing combination. Happy testing!
Follow our blog
Be the first to know when we publish new content.