Cucumber Usage Report

Profile picture for user devraj

If we are more concerned about the time taken by each Step Definition, then we should use the usage plugin. This is how we specify the same in @CucumberOptions:

@CucumberOptions( plugin = { "usage"})

Complete Code

@CucumberOptions(
    plugin = {"usage"},
    features = {"features"},
    glue={"com.pb.cucumbertest.stepdefinitions"},
    monochrome = true,
    strict = true,
    dryRun = false,
    tags = {"@singleargument"}
)

This sorts the Step Definitions by their average execution time. The output from the usage plugin is useful for quickly finding slow parts in your code but it is also a great way to get an overview of your Step Definitions.

Sample Output:

[
  {
    "source": "^(?:I am|User is) on the home page$",
    "steps": [
      {
        "name": "I am on the home page",
        "aggregatedDurations": {
          "median": 0.295168,
          "average": 0.217027
        },
        "durations": [
          {
            "duration": 0.585281,
            "location": "file:///Users/ContactUs.feature:4"
          },
          {
            "duration": 0.295168,
            "location": "file:///Users/ContactUs.feature:4"
          },
          {
            "duration": 0.770632,
            "location": "file:///Users/Login.feature:5"
          }
        ]
      }
    ]
  }
]

Average and Median Functions in Cucumber Libraries

/**
     * Calculate the average of a list of duration entries
     */
    Duration calculateAverage(List<Duration> durationEntries) {

        Duration sum = Duration.ZERO;
        for (Duration duration : durationEntries) {
            sum = sum.plus(duration);
        }
        if (sum.isZero()) {
            return Duration.ZERO;
        }

        return sum.dividedBy(durationEntries.size());
    }

    /**
     * Calculate the median of a list of duration entries
     */
    Duration calculateMedian(List<Duration> durationEntries) {
        if (durationEntries.isEmpty()) {
            return Duration.ZERO;
        }
        Collections.sort(durationEntries);
        int middle = durationEntries.size() / 2;
        if (durationEntries.size() % 2 == 1) {
            return durationEntries.get(middle);
        } else {
            Duration total = durationEntries.get(middle - 1).plus(durationEntries.get(middle));
            return total.dividedBy(2);
        }
    }