Tuesday 30 September 2014

Cucumber Reports - With Selenium and Junit



Cucumber Reports

Reports are important part of any project and cucumber provides default reports. Cucumber can be integrated with other open source tools like ANT,Jenkins etc
Cucumber can report results in several different formats, using formatters. Formatters are specified either on the command line or in code.

Gherkin is the language that Cucumber understands. It is a Business Readable, Domain Specific Language that lets you describe software's behavior without detailing how that behavior is implemented. Gherkin serves two purposes — documentation and automated tests.

Below are 3 reports explained in Detail :

A) Pretty Format (HTML reports)
B) JSON Reports
C) Junit Reports

A) Pretty format (HTML Reports)
Prints the gherkin source with additional colours and stack traces for errors.
Create a class file in src\main\java folder in the project. Paste the below code and Running the below runner file as junit will generate below reports in HTML format.

Code in runner file
package test.cucumber;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(format = {"html:target/cucumber-html-report", "json:target/cucumber-json-" +"report.json"})
public class RunBrowserTest {
          }

The above code will generate below reports in the project location in target\cucumber HTML Reports folder.
Eg: D:\Workspace\cucumber-jvm\target\cucumber-html-report


B) JSON Reports
This report contains all the information from the gherkin source, with additional results for each step, including embedded screenshots. This report is meant to be post-processed into another visual format by 3rd party tools such as Cucumber Jenkins.
Create a class file in src\main\java folder in the project. Paste the below code and Running the below runner file as junit will generate below reports in JSON format.
package test.cucumber;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@Cucumber.Options(format = {"json:target/cucumber.json"})
public class RunBrowserTest {
          }

Below is the example of JSON report opened with notepad.




C) JUnitReport
Merge the individual XML files generated by the JUnit task and eventually apply a stylesheet on the resulting merged document to provide a browsable report of the testcases results.

Create a class file in src\main\java folder in the project. Paste the below code and Running the below runner file as junit will generate below reports in Junit.xml format.
package test.cucumber;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@Cucumber.Options(format = {"junit:taget_junit/cucumber.xml"})
public class RunBrowserTest {
          }

Below is the example of Junit report opened with notepad++.


1 comment: