Cucumber (Gherkin) Running Cucumber Tests & Generating Reports Estimated reading: 5 minutes 175 views Setting up Hooks and Tags in Cucumber is just the beginning of efficient test automation. Once your environment is configured, the next step is to explore the various methods available for executing your Cucumber scripts.Each method has its unique advantages and use cases. In this guide, we will delve into three primary ways to run your Cucumber tests: The Feature File - This method is ideal for quickly running specific features during development. JUnit Test - This method is suitable for developers looking to perform rapid testing of their scripts with minimal setup. Maven using Terminal - This method is perfect for integrating into continuous integration/continuous deployment.Running as a Cucumber FeatureThe first method to execute your Cucumber script is directly from the feature file. This method is straightforward and does not consider tags. Running As A Cucumber FeatureHow to Execute: Right-click on the feature file and select Run As > Cucumber Feature.Best Use Case: This method is ideal for quickly running specific features during development, allowing you to verify functionality without the need for additional configurations. Running as a Cucumber FeatureThis method does not allow for the use of tags, which can be limiting if you need to run specific subsets of tests.Running as a JUnit TestThe second method involves running your Cucumber scripts using JUnit, which is great for quick testing. Running As A JUnit TestHow to Execute: Open the Runner File and select Run As > JUnit Test.Best Use Case: This method is suitable for testers looking to perform rapid testing of their scripts with minimal setup. Running as a JUnit TestPrimarily used within a local development setup and may not be suitable for larger, continuous integration environments.Configuring Cucumber OptionsOne of the standout features of Cucumber is its ability to integrate with various plugins, which allows users to generate a variety of reports.Common Plugin Options html: This plugin generates a comprehensive HTML report, providing a clear and visually appealing overview of your test results. json: The JSON plugin creates a structured JSON report, offering detailed insights that can be easily integrated into other tools or systems for further analysis. junit: The JUnit plugin produces a standard JUnit XML report, ensuring compatibility with various testing frameworks and continuous integration tools. extents adapter: This plugin generates a visually stunning HTML report with rich formatting and detailed analytics, making it easy to share and present your test outcomes.CucumberOptions@CucumberOptions( features = "src/test/resources/features", glue = "com.masteringqa.stepdefinitions", tags = "@smoketest", plugin = {}, monochrome = true, )Generating ReportsCucumber can generate various types of reports. The most commonly used are HTML and JSON reports.HTML ReportHTML reports are user-friendly and provide a detailed overview of test scenarios, steps, and their outcomes.HTML Report@CucumberOptions( features = "src/test/resources/features", glue = "com.masteringqa.stepdefinitions", tags = "@smoketest", plugin = {"pretty","html:target/cucumber-reports.html"}, monochrome = true, ) Example: HTML ReportJSON ReportJSON reports are useful for integrating Cucumber test results with other tools, such as CI/CD pipelines or dashboards. These reports provide a structured format that can be easily parsed and processed.JSON Reports@CucumberOptions( features = "src/test/resources/features", glue = "com.masteringqa.stepdefinitions", tags = "@smoketest", plugin = {"pretty","json:target/cucumber-reports.json"} monochrome = true, ) Example: JSON ReportExtent HTML ReportThe ExtentCucumberAdapter plugin is a highly versatile and essential tool for modern test reporting, seamlessly integrating with your testing pipeline to deliver a cutting-edge HTML reporting design. Its interactive, visually appealing format transforms test results into clear and actionable insights, making it indispensable for enhancing the efficiency and transparency of your testing processes.Extent HTML Report@CucumberOptions( features = "src/test/resources/features", glue = "com.masteringqa.stepdefinitions", tags = "@smoketest", plugin = {"com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:"}, monochrome = true, ) Example: Extent HTML Report Ensure you add the plugin to your runner before proceeding!Once the plugin is added, execute the mvn test command to run your tests. After the tests complete, you can view the detailed report generated by the ExtentsCucumberAdapter. This will also work for Running as a JUnit Test method.Running from Terminal using MavenThe third method is running your scripts from the terminal using the mvn test command with the Surefire plugin, ideal for pipeline integration. In this scenario we’ll use the extents report. Running The Test Using Maven Command in TerminalTerminal: mvn test<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <includes> <include>**/HomeScreenRunner.java</include> </includes> </configuration> </plugin> How to Execute: Open your terminal and navigate to your project directory. Use the command: mvn test. Best Use Case: This method is perfect for integrating into continuous integration/continuous deployment (CI/CD) pipelines, ensuring that tests are automatically run as part of the build process. Maven Test Results Example: Extent HTML Report Tip: Initial Setup: Requires Maven and the Surefire plugin to be correctly set up, which may involve additional configuration.ConclusionUnderstanding the various methods to execute your Cucumber scripts is essential for efficient testing and integration into your development workflow. Whether you choose to run scripts directly from the feature file for quick checks, utilize JUnit for structured testing, or integrate with Maven for CI/CD pipelines, each method has its own unique advantages. By leveraging these techniques, you can ensure thorough testing of your application, leading to higher quality software and a more streamlined development process.Tagged:Cucumber Cucumber (Gherkin) - Previous Understanding Cucumber Hooks