Create Cucumber JVM custom formatter

Last Updated on by

Post summary: How to implement custom formatter for Cucumber JVM.

Cucumber code examples can be found in selenium-samples-java/cucumber-parallel GitHub repository.

Cucumber has different types of formatters available. See Cucumber formatters for more details. Still, those might not fulfill your needs. If this is the case Cucumber provides a way to create own custom formatter.

Why create Cucumber JVM custom formatter

There could be different reasons for this. Standard formatters are known not to be thread safety, so one reason could be to create own thread safety formatter. Other reason could be if you want your results to be stored directly into a database or sent to a REST service. There could be many reasons, reading this post means you already have one.

How to create Cucumber JVM custom formatter

Custom formatter can be created by implementing two interfaces: gherkin.formatter.Reporter and gherkin.formatter.Formatter. This is it. You have to know though when each method from interfaces is called and this is pretty tricky. One simple implementation of custom formatter can be found in GitHub file. This is implementation from scratch. Another option is to extend some already existing formatter and modify its behavior.

Using custom formatter in Cucumber runner

Custom formatter is invoked by fully qualified class name in @CucumberOptions annotation. In this example it is: plugin = {“com.automationrhapsody.cucumber.formatter.CustomFormatter”}.

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

		plugin = {
public class RunWikipediaTestsWithCustomFormatter {

Passing an argument to custom formatter

In the code above along with a fully qualified class name of CustomFormatter there is also file name (and location) with colon – :custom-formatter-output.txt.  This is the way to pass argument to CustomFormatter. In order to receive the argument CustomFormatter constructor should have one of the following parameters:

  • null – empty constructor, this is just to instantiate some variable, nothing can be received from outside.
  • interface java.lang.Appendable – object to which values should be appended. It should have an implementation which can be custom or gherkin.formatter.NiceAppendable can be used. See example below.
  • class – URI that can be used in case of database connection strings or URL. Also applicable for passing a plain String.
  • class – only for web URL.
  • class – file path can be passed, could be absolute or relative.


public CustomFormatter(Appendable appendable) {
	output = new NiceAppendable(appendable);
	System.out.println("CustomFormatter(): " + output.toString());


Cucumber JVM gives a lot of flexibility by providing a way to implement custom formatter based on your current needs. It is pretty straightforward to do it. It is possible also to pass arguments to a parametrized constructor in custom formatter.

Category: Java | Tags: