Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 66 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ This tool is configured as a build step before the "ALM Octane Cucumber test rep

The following frameworks are currently supported out-of-the-box:
- cucumber-js
> verfied versions: 4.2.1, 5.1.0, 6.0.5, 7.3.0
> tested version: 13.3.0, with the built-in junit formatter (since bdd2octane 1.1.14)
- php-behat
> verified versions: 3.8.1
> tested version: 4.1.1, with the junit formatter (since bdd2octane 1.1.14)
- python-radish
> verified versions: 0.13.2, 0.13.4
- python-behave
> verified versions: 1.2.5, 1.2.6
> tested version: 1.3.3, with the JUnit reporter (since bdd2octane 1.1.14)
- cucumber-jvm
> verifed versions: 2.0.0 - 7.0.0, 7.23.0
> unsupported version: 1.2.6
> tested version: 8.1.0, with the junit plugin, and with Maven Surefire 3.6.1 and JUnit 4 or JUnit 5 (since bdd2octane 1.1.14)
- cucumber-ruby
> verified versions: 4.0.0, 5.0.0, 6.0.0, 7.0.0
> tested version: 12.0.1, with the junit formatter (since bdd2octane 1.1.14)

You can add your own framework by implementing the interface: [BddFrameworkHandler](./src/main/java/com/microfocus/bdd/api/BddFrameworkHandler.java)
SpecFlow test result injection is supported via the use of a template file for report generation. See https://marketplace.opentext.com/appdelivery/content/alm-octane-bdd-automation-with-specflow.
Expand Down Expand Up @@ -61,13 +60,53 @@ SpecFlow test result injection is supported via the use of a template file for r

### Notes on Cucumber-js handler

* Cucumber-js using the Gherkin library to parse the feature file. For Cucumber-js version 6.x and below, it uses the Gherkin
library in version 5 or below which does not support the keyword "Example".
If a feature file contains "Example" as keyword, the program will throw an error.
It is recommended to use "Scenario" instead of "Example" as the scenario keyword to avoid this error.
* "Example" keyword was added to the Gherkin library in version 6.0.13 on 9/25
2018 [Gherkin library changelog](https://github.com/cucumber/common/blob/main/gherkin/CHANGELOG.md#6013---2018-09-25).
Cucumber-js version 7 and above applies the updated Gherkin library and won't have this problem.
* The handler reads the report of the built-in junit formatter of Cucumber-js, for example
`cucumber-js --format junit:reports/junit.xml`: one testcase per scenario or Examples row, with the step results
in system-out. The report of the `cucumber-junit` converter (one testsuite per scenario), used with Cucumber-js 7
and earlier, is no longer supported.
* The report has no feature file path, so scenarios are matched to feature files by feature name. Feature names must be
unique among the feature files.
* The report has no step durations, so steps are reported with a duration of 0.
* A scenario that failed outside its steps, for example in a Before or After hook, is reported with skipped steps and
a warning, because the Octane result can only report failures of steps.
* A failed BeforeAll or AfterAll hook is reported as a testcase of no scenario. It is skipped with a message, so
Octane does not show the hook failure.

### Notes on Cucumber-ruby handler

* The handler reads the report of the junit formatter of Cucumber-ruby, for example
`cucumber --format junit --out reports/junit`.
* The report has no step results or durations. The failed step is the step at the line of the failure message, the
steps before it are reported as passed and the steps after it as skipped. Steps are reported with a duration of 0.
* An Examples row is identified by its values. Rows with the same values are matched in the order of the report, so in
a report of only some of them, for example a rerun, a result can be assigned to another row with the same values.
* A scenario that failed outside its steps, for example in a Before or After hook, is reported with skipped steps and
a warning, because the Octane result can only report failures of steps.

### Notes on Python-behave handler

* The handler reads the report of the JUnit reporter of behave, for example
`behave features --junit --junit-directory reports/junit`. The status and duration of each step are read from the
scenario text in system-out.
* An Examples row is identified by the `@<examples>.<row>` of its name, as in the default
`scenario_outline_annotation_schema`. With another schema, the rows are matched in the order of the report.
* The failure of a before_scenario or after_scenario hook is reported on the first step that did not pass, or on the
last step if all of them passed, or on the first step if the report has no step results. If the scenario has no steps,
the conversion fails with an error.
* The report does not record the failure of a before_feature or after_feature hook.

### Notes on PHP-behat handler

* The handler reads the report of the junit formatter of Behat, for example `behat --format=junit --out=reports/junit`.
* The report has no step results or durations. The failed step is the step named in the failure message, the steps
before it are reported as passed and the steps after it as skipped, with a duration of 0. Of repeated steps with the
same keyword and text, the first one is reported as failed.
* An Examples row is identified by its name, which has the values of the row, and by its failed step. Rows with the same
name are matched in the order of the report.
* The failure of a hook before the steps, for example BeforeScenario or BeforeFeature, is reported on the first step,
and the failure of a hook after the steps on the last step. If the scenario has no steps, the conversion fails with
an error.
* The report does not record the failure of a BeforeStep or AfterStep hook.

### Notes on Cucumber-jvm handler
* If **useFileNameCompatibleName** is enabled, the space character in a feature name is replaced with underscore(_) in JUnit report,
Expand All @@ -82,3 +121,17 @@ It is required to enable the "pretty" option of Cucumber-jvm. This tool will par
currently the handler doesn't support this configuration.

* When using JUnit 5.x, you must configure the system property `cucumber.junit-platform.naming-strategy=long` to ensure proper test naming. Refer to the [official documentation for integration details with Maven Surefire and Gradle](https://github.com/cucumber/cucumber-jvm/blob/main/cucumber-junit-platform-engine/README.md#maven-surefire-and-gradle).

* The handler reads the report of the junit plugin, for example `cucumber.plugin=junit:target/cucumber.xml`, and the
Surefire reports of the JUnit 4 and JUnit 5 runners. The junit plugin report has the status of each step. With a
Surefire report, the failed step is read from the failure, and the steps before it are reported as passed. The
reports have no step durations, so steps are reported with a duration of 0.
* Surefire 3.5.3 and later write the class of the runner as classname. With JUnit 5, the feature is then found by the
name of the long naming strategy, which starts with the feature name. With JUnit 4, it is found by the location of
the scenario that the "pretty" plugin writes to \<system-out>.
* An Examples row is identified by its numbers in the name, for example `#1.2`, or with JUnit 4 by its line in the
output of the "pretty" plugin.
* If a scenario failed, but no failed step can be read from the report, for example because a hook or the setup of
the test failed, all its steps are reported as skipped and a warning is printed. A failed scenario is never reported
as passed. To read the failed step from a Surefire report, keep the `trimStackTrace` option of Surefire `false`,
which is its default.
5 changes: 4 additions & 1 deletion src/main/java/com/microfocus/bdd/Bdd2Octane.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ public void run() throws IOException, XMLStreamException, InstantiationException
}

//3. Merge steps into a scenario.
Optional<OctaneScenario> scenario = octaneFeature.findScenarioAndUpdateOutlineIndex(scenarioName);
OptionalInt outlineIndex = bddFrameworkHandler.getOutlineIndex();
Optional<OctaneScenario> scenario = outlineIndex.isPresent()
? octaneFeature.findScenario(scenarioName, outlineIndex.getAsInt())
: octaneFeature.findScenarioAndUpdateOutlineIndex(scenarioName);
if (!scenario.isPresent()) {
System.err.println("cannot find scenario: " + scenarioName + " from feature");
skipped++;
Expand Down
192 changes: 125 additions & 67 deletions src/main/java/com/microfocus/bdd/CucumberJsHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,34 @@
package com.microfocus.bdd;

import com.microfocus.bdd.api.*;
import io.cucumber.messages.types.Examples;
import io.cucumber.messages.types.FeatureChild;
import io.cucumber.messages.types.Rule;
import io.cucumber.messages.types.RuleChild;
import io.cucumber.messages.types.Scenario;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Reads a testcase of the built-in junit formatter of Cucumber-js. The testcase is named
* "[rule - ]scenario[ - examples - #e.r[: pickle name]]" and its system-out has one
* "keyword step text....status" line per step. Step durations are not reported.
*/
public class CucumberJsHandler implements BddFrameworkHandler {
private static final Pattern STEP_RESULT = Pattern.compile(".+\\.\\.(passed|failed|skipped|pending|undefined|ambiguous|unknown)");

private Element element;
private String failureStep;
private String errorMessage;
private Optional<Long> failureDuration;
private Map<String, Optional<Long>> passedStepWithDuration;
private boolean isSkipped = false;
private List<String> stepResults;
private String failure;
private boolean failedOutsideSteps;
private int nextStepResult;
private int outlineIndex;

@Override
public String getName() {
Expand All @@ -51,92 +69,132 @@ public String getName() {
@Override
public void setElement(Element element) {
this.element = element;
passedStepWithDuration = new HashMap<>();
if(!element.getChildren().isEmpty()) {
for(Element stepElement: element.getChildren()) {
if(stepElement.getName().equals("testcase")) {
if(stepElement.getChildren().isEmpty()) {
passedStepWithDuration.put(stepElement.getAttribute("name"), convertToLong(stepElement.getAttribute("time")));
} else if (stepElement.getChildren().get(0).getName().equals("failure")) {
Element failedStep = stepElement.getChildren().get(0);
failureStep = stepElement.getAttribute("name");
errorMessage = failedStep.getText();
failureDuration = convertToLong(failedStep.getAttribute("time"));
} else if (stepElement.getChildren().get(0).getName().equals("skipped")) {
isSkipped = true;
}
}
}
}
}

private Optional<Long> convertToLong(String time) {
if(time != null) {
Double value = Double.parseDouble(time);
return Optional.of(Math.round(value * 1000));
}
return null;
List<String> lines = element.getChild("system-out")
.map(systemOut -> Arrays.stream(systemOut.getText().split("\n")).map(String::trim)
.filter(line -> !line.isEmpty()).collect(Collectors.toList()))
.orElse(new ArrayList<>());
stepResults = lines.stream().allMatch(line -> STEP_RESULT.matcher(line).matches()) ? lines : new ArrayList<>();
failure = element.getChild("failure")
.map(f -> f.getText().isEmpty() && f.getAttribute("message") != null ? f.getAttribute("message") : f.getText())
.orElse(null);
// e.g. a hook failed: the testcase failed, but none of its steps
failedOutsideSteps = (element.getChild("failure").isPresent() || element.getChild("error").isPresent())
&& stepResults.stream().noneMatch(line -> toStatus(line) == Status.FAILED);
nextStepResult = 0;
outlineIndex = 0;
}

@Override
public Optional<String> getFeatureName(OctaneFeatureLocator... octaneFeatureLocator) {
return Optional.empty();
return Optional.ofNullable(element.getAttribute("classname"));
}

@Override
public Optional<String> getFeatureFile() {
return findFeatureFile();
return Optional.empty();
}

/**
* @return the name of the scenario or scenario outline that the testcase is named after, null if there is none
*/
@Override
public String getScenarioName(OctaneFeature feature, OctaneFeatureLocator... octaneFeatureLocator) {
String featureName = feature.getName();
String featureNameWithDash = featureName.replaceAll(" ", "-");
featureNameWithDash = featureNameWithDash.substring(0, 1).toLowerCase() + featureNameWithDash.substring(1);
String elementName = element.getAttribute("name");
String scenarioNameFromElement = elementName.replace(featureNameWithDash.toLowerCase(), "").substring(1);
for (OctaneScenario scenario : feature.getScenarios()) {
String currentScenarioName = scenario.getOutlineName();
String changedScenarioName = currentScenarioName.replace(" ", "-").replace(" ", "");
if (changedScenarioName.equalsIgnoreCase(scenarioNameFromElement)) {
return scenario.getName();
Optional<OctaneScenario> scenario = findScenario(feature);
outlineIndex = scenario.map(OctaneScenario::getOutlineIndex).orElse(0);
return scenario.map(OctaneScenario::getName).orElse(null);
}

@Override
public OptionalInt getOutlineIndex() {
return outlineIndex > 0 ? OptionalInt.of(outlineIndex) : OptionalInt.empty();
}

private Optional<OctaneScenario> findScenario(OctaneFeature feature) {
String name = element.getAttribute("name");
for (FeatureChild child : feature.getGherkinDocument().getFeature().get().getChildren()) {
if (child.getScenario().isPresent()) {
Optional<OctaneScenario> scenario = findScenario(feature, name, "", child.getScenario().get());
if (scenario.isPresent()) {
return scenario;
}
}
if (child.getRule().isPresent()) {
Rule rule = child.getRule().get();
for (RuleChild ruleChild : rule.getChildren()) {
if (ruleChild.getScenario().isPresent()) {
Optional<OctaneScenario> scenario = findScenario(feature, name, rule.getName(), ruleChild.getScenario().get());
if (scenario.isPresent()) {
return scenario;
}
}
}
}
}
return Optional.empty();
}

private static Optional<OctaneScenario> findScenario(OctaneFeature feature, String name, String ruleName, Scenario scenario) {
String scenarioName = join(ruleName, scenario.getName());
if (scenario.getExamples().isEmpty()) {
return scenarioName.equals(name) ? feature.findScenario(scenario.getName(), 0) : Optional.empty();
}
int outlineIndex = 0;
for (int e = 0; e < scenario.getExamples().size(); e++) {
Examples examples = scenario.getExamples().get(e);
for (int r = 0; r < examples.getTableBody().size(); r++) {
outlineIndex++;
String exampleName = join(scenarioName, examples.getName(), "#" + (e + 1) + "." + (r + 1));
if (name.equals(exampleName) || name.startsWith(exampleName + ": ")) {
return feature.findScenario(scenario.getName(), outlineIndex);
}
}
}
return null;
return Optional.empty();
}

private static String join(String... names) {
return Arrays.stream(names).filter(name -> !name.isEmpty()).collect(Collectors.joining(" - "));
}

/**
* Sets the status of the step from the next step line.
*/
@Override
public void fillStep(OctaneStep octaneStep) {
String stepWithKeyWord = octaneStep.getCompleteStepName();
if(failureStep != null && failureStep.equals(stepWithKeyWord)) {
octaneStep.setStatus(Status.FAILED);
octaneStep.setErrorMessage(errorMessage);
if(failureDuration != null && failureDuration.isPresent()) {
octaneStep.setDuration(failureDuration.get());
}
} else if (passedStepWithDuration.containsKey(stepWithKeyWord)) {
octaneStep.setStatus(Status.PASSED);
if (passedStepWithDuration.get(stepWithKeyWord) != null) {
octaneStep.setDuration(passedStepWithDuration.get(stepWithKeyWord).get());
}
} else if (isSkipped) {
if (failedOutsideSteps) {
System.err.println("Scenario \"" + element.getAttribute("name")
+ "\" failed outside its steps, its steps are reported as skipped");
octaneStep.setStatus(Status.SKIPPED);
return;
}
String stepResult = nextStepResult < stepResults.size() ? stepResults.get(nextStepResult++) : "";
if (!stepResult.startsWith(octaneStep.getCompleteStepName() + "..")) {
System.err.println("Step \"" + octaneStep.getCompleteStepName() + "\" of scenario \""
+ element.getAttribute("name") + "\" is not in the report, it is reported as skipped");
octaneStep.setStatus(Status.SKIPPED);
return;
}
octaneStep.setStatus(toStatus(stepResult));
if (octaneStep.getStatus() == Status.FAILED) {
octaneStep.setErrorMessage(failure == null || failure.isEmpty()
? "The step is " + stepResult.substring(stepResult.lastIndexOf('.') + 1) : failure);
}
}

public Optional<String> findFeatureFile() {
Element propertiesElements = element.getChild("properties").get();
for (Element property: propertiesElements.getChildren()){
if (property.getAttribute("name").equals("URI")) {
return Optional.of(property.getAttribute("value"));
}

private static Status toStatus(String stepResult) {
switch (stepResult.substring(stepResult.lastIndexOf('.') + 1)) {
case "passed":
return Status.PASSED;
case "skipped":
case "unknown":
return Status.SKIPPED;
default:
return Status.FAILED;
}
return Optional.empty();
}

@Override
public String getTestCaseElementName() {
return "testsuite";
return "testcase";
}
}
}
Loading
Loading