The previous tutorial is all about JUnit Test Runner. In this tutorial, will show you how to generate reports in Cucumber.
Cucumber with JUnit gives us the capability to generate reports in the form of HTML, XML, JSON & TXT. Cucumber frameworks generate very good and detailed reports, which can be share with all stakeholders.
Let’s generate the Cucumber Report
Step 1 − Create a Maven project named MyCucumberProject in Eclipse.
Step 2 − Create a feature file named MyHoliday.feature under src/test/resources
Step 3 − Create a StepDefinition named MyHolidayDefinitions.java under src/test/java
Step 4 − Create a TestRunner class under src/test/resources
Step 5 − Run JUnit TestRunner class by right click Run As -> JUnit
Write the following code for Feature File
Feature: Book Flight and Hotel for Vacation
@BookOneWayFlight
Scenario: Book Flight for one way trip
Given I live in Dublin with 2 adults and 2 kids
And I want to book one way flight ticket from Dublin to London on 22nd Jan 2020
When I search online
Then TripAdvisor should provide me options of flights on 22nd Jan 2020
And Cost of my flight should not be more than 50 Euro per person
And Tickets should be refundable
@BookHotel
Scenario: Book Hotel for one the trip
Given I need 1 room with 2 double beds
And I want to book hotel from 22nd Jan 2020 to 25th Jan 2020
When I search online
Then TripAdvisor should provide me options of hotels for time period of 22nd Jan 2020 to 25th Jan 2020
And Fare of my room should not be more than 200 Euro per night
And Breakfast should be included in the room fare

Below is the full program, which shows the step definition of above mentioned feature
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
public class MyHolidayDefinitions {
@Given("^I live in Dublin with 2 adults and 2 kids$")
public void needFlight() {
System.out.println("I live in Dublin with 2 adults and 2 kids");
}
@Given("^I need 1 room with 2 double beds$")
public void needRoom() {
System.out.println("I need 1 room with 2 double beds");
}
@When("^I search online$")
public void onlineSearch() {
System.out.println("I search online");
}
@Then("^TripAdvisor should provide me options of flights on 22nd Jan 2020$")
public void searchFlightInTripAdvisor() {
System.out.println("TripAdvisor should provide me options of flights on 22nd Jan 2020");
}
@Then("^Cost of my flight should not be more than 50 Euro per person$")
public void flightFare() {
System.out.println("Cost of my flight should not be more than 50 Euro per person");
}
@Then("^TripAdvisor should provide me options of hotels for time period of 22nd Jan 2020 to 25th Jan 2020")
public void searchRoomInTripAdvisor() {
System.out.println(
"TripAdvisor should provide me options of hotels for time period of 22nd Jan 2020 to 25th Jan 2020");
}
@And("^I want to book one way flight ticket from Dublin to London on 22nd Jan 2020$")
public void ticketType() {
System.out.println("I want to book one way flight ticket from Dublin to London on 22nd Jan 2020");
}
@And("^I want to book hotel from 22nd Jan 2020 to 25th Jan 2020$")
public void stayDuration() {
System.out.println("I want to book hotel from 22nd Jan 2020 to 25th Jan 2020");
}
@And("^Fare of my room should not be more than 200 Euro per night$")
public void roomFare() {
System.out.println("Fare of my room should not be more than 200 Euro per night");
}
@And("^Breakfast should be included in the room fare$")
public void includeBreakfast() {
System.out.println("Breakfast should be included in the room fare");
}
@And("^Tickets should be refundable$")
public void refunableTicket() {
System.out.println("Tickets should be refundable");
}
}


Cucumber HTML Reports
For HTML reports, add html:target/cucumber-reports to the @CucumberOptions plugin option.
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/Feature/MyHoliday.feature",
plugin = { "pretty","html:target/cucumber-reports" }, tags = { "" })
public class TestRunner {
}
We have specified the path of the Cucumber report, which we want it to generate it under the target folder. This will generate an HTML report at the location mentioned in the formatter

HTML Report Output

Cucumber Json Reports
For Json reports, add json:target/cucumber-reports/Cucumber.json to the @CucumberOptions plugin option.
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/Feature/MyHoliday.feature",
plugin = { "pretty","json:target/cucumber-reports/Cucumber.json" }, tags = { "" })
public class TestRunner {
}

JSON Report Output

Cucumber JUNIT XML Report
For JUNIT reports, add junit:targe/cucumber-reports/Cucumber.xml to the @CucumberOptions plugin option.
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/Feature/MyHoliday.feature",
plugin = { "pretty","junit:target/cucumber-reports/Cucumber.xml" }, tags = { "" })
public class TestRunner {
}


Cucumber JUnit XML Report
