Skip to main content

Cucumber Usage Report

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);
        }
    }

Submitted by tgoswami on August 30, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.