diff --git a/karate-core/src/main/java/com/intuit/karate/Results.java b/karate-core/src/main/java/com/intuit/karate/Results.java index fa7d06bb9..d0fd7298c 100644 --- a/karate-core/src/main/java/com/intuit/karate/Results.java +++ b/karate-core/src/main/java/com/intuit/karate/Results.java @@ -50,8 +50,8 @@ public class Results { private final int scenariosFailed; private final double timeTakenMillis; private final long endTime; - private final List errors = new ArrayList(); - private final List> featureSummary = new ArrayList(); + private final List errors = new ArrayList<>(); + private final List> featureSummary = new ArrayList<>(); public static Results of(Suite suite) { return new Results(suite); @@ -138,7 +138,7 @@ private void printStats() { } public Map toKarateJson() { - Map map = new HashMap(); + Map map = new HashMap<>(); map.put("version", FileUtils.KARATE_VERSION); map.put("env", suite.env); map.put("threads", suite.threadCount); diff --git a/karate-core/src/main/java/com/intuit/karate/core/FeatureResult.java b/karate-core/src/main/java/com/intuit/karate/core/FeatureResult.java index d2f5a028c..14071b002 100644 --- a/karate-core/src/main/java/com/intuit/karate/core/FeatureResult.java +++ b/karate-core/src/main/java/com/intuit/karate/core/FeatureResult.java @@ -70,7 +70,7 @@ public void printStats() { } public List getAllEmbedFiles() { - List files = new ArrayList(); + List files = new ArrayList<>(); for (ScenarioResult sr : scenarioResults) { for (StepResult stepResult : sr.getStepResults()) { if (stepResult.getEmbeds() != null) { @@ -105,7 +105,7 @@ public static FeatureResult fromKarateJson(File workingDir, Map } public Map toInfoJson() { - Map map = new HashMap(); + Map map = new HashMap<>(); map.put("name", feature.getName()); map.put("description", feature.getDescription()); map.put("prefixedPath", feature.getResource().getPrefixedPath()); @@ -118,7 +118,7 @@ public Map toInfoJson() { } public Map toSummaryJson() { - Map map = new HashMap(); + Map map = new HashMap<>(); map.put("failed", isFailed()); map.put("name", feature.getName()); map.put("description", feature.getDescription()); @@ -132,7 +132,7 @@ public Map toSummaryJson() { } public Map toKarateJson() { - Map map = new HashMap(); + Map map = new HashMap<>(); // these first few are only for the ease of reports // note that they are not involved in the reverse fromKarateJson() map.put("name", feature.getName()); @@ -148,7 +148,7 @@ public Map toKarateJson() { } map.put("resultDate", resultDate); map.put("prefixedPath", feature.getResource().getPrefixedPath()); - List> list = new ArrayList(scenarioResults.size()); + List> list = new ArrayList<>(scenarioResults.size()); map.put("scenarioResults", list); for (ScenarioResult sr : scenarioResults) { list.add(sr.toKarateJson()); @@ -163,7 +163,7 @@ public Map toKarateJson() { } public Map toCucumberJson() { - Map map = new HashMap(); + Map map = new HashMap<>(); map.put("keyword", Feature.KEYWORD); map.put("line", feature.getLine()); map.put("uri", displayName); @@ -177,7 +177,7 @@ public Map toCucumberJson() { if (feature.getTags() != null) { map.put("tags", ScenarioResult.tagsToCucumberJson(feature.getTags())); } - List> list = new ArrayList(scenarioResults.size()); + List> list = new ArrayList<>(scenarioResults.size()); map.put("elements", list); for (ScenarioResult sr : scenarioResults) { Map backgroundMap = sr.backgroundToCucumberJson(); @@ -190,7 +190,7 @@ public Map toCucumberJson() { } public List getAllScenarioStepResultsNotHidden() { - List list = new ArrayList(); + List list = new ArrayList<>(); for (ScenarioResult sr : scenarioResults) { list.addAll(sr.getStepResultsNotHidden()); } @@ -286,7 +286,7 @@ public boolean isFailed() { } public List getErrors() { - List errors = new ArrayList(); + List errors = new ArrayList<>(); for (ScenarioResult sr : scenarioResults) { if (sr.isFailed()) { errors.add(sr.getErrorMessage()); @@ -305,7 +305,7 @@ public void setVariables(Map resultVariables) { public Map getVariables() { // edge case if no scenarios were run - return resultVariables == null ? new HashMap() : resultVariables; + return resultVariables == null ? new HashMap<>() : resultVariables; } public void setConfig(Config config) { diff --git a/karate-core/src/main/java/com/intuit/karate/core/ScenarioResult.java b/karate-core/src/main/java/com/intuit/karate/core/ScenarioResult.java index 384f6c8d4..247e0bf1a 100644 --- a/karate-core/src/main/java/com/intuit/karate/core/ScenarioResult.java +++ b/karate-core/src/main/java/com/intuit/karate/core/ScenarioResult.java @@ -189,6 +189,13 @@ public static ScenarioResult fromKarateJson(File workingDir, Feature feature, Ma } scenario.setSteps(steps); } + + if (scenario.getTagsEffective().contains(Tag.FAIL) && sr.isFailed()) { + if (!sr.getErrorMessage().startsWith(ScenarioRuntime.EXPECT_TEST_TO_FAIL_BECAUSE_OF_FAIL_TAG)) { + sr.ignoreFailedStep(); + } + + } return sr; } diff --git a/karate-core/src/main/java/com/intuit/karate/core/ScenarioRuntime.java b/karate-core/src/main/java/com/intuit/karate/core/ScenarioRuntime.java index 76fee3fdb..872122317 100644 --- a/karate-core/src/main/java/com/intuit/karate/core/ScenarioRuntime.java +++ b/karate-core/src/main/java/com/intuit/karate/core/ScenarioRuntime.java @@ -30,7 +30,6 @@ import com.intuit.karate.ScenarioActions; import com.intuit.karate.StringUtils; import com.intuit.karate.graal.JsEngine; -import com.intuit.karate.graal.JsValue; import com.intuit.karate.http.ResourceType; import com.intuit.karate.shell.StringLogAppender; @@ -73,14 +72,14 @@ public ScenarioRuntime(FeatureRuntime featureRuntime, Scenario scenario) { perfMode = featureRuntime.perfHook != null; if (caller.isNone()) { logAppender = new StringLogAppender(false); - engine = new ScenarioEngine(caller.getParentConfig(false), this, new HashMap(), logger); + engine = new ScenarioEngine(caller.getParentConfig(false), this, new HashMap<>(), logger); } else if (caller.isSharedScope()) { logAppender = caller.parentRuntime.logAppender; engine = new ScenarioEngine(caller.getParentConfig(false), this, caller.getParentVars(false), logger); } else { // new, but clone and copy data logAppender = caller.parentRuntime.logAppender; // in this case, parent variables are set via magic variables - see initMagicVariables() - engine = new ScenarioEngine(caller.getParentConfig(true), this, new HashMap(), logger); + engine = new ScenarioEngine(caller.getParentConfig(true), this, new HashMap<>(), logger); } logger.setAppender(logAppender); actions = new ScenarioActions(engine); @@ -97,7 +96,7 @@ public ScenarioRuntime(FeatureRuntime featureRuntime, Scenario scenario) { if (featureRuntime.setupResult != null) { // TODO improve this and simplify report rendering code in report/karate-feature.html StepResult sr = result.addFakeStepResult("@setup", null); - List list = new ArrayList(1); + List list = new ArrayList<>(1); FeatureResult fr = new FeatureResult(featureRuntime.featureCall.feature); fr.setCallDepth(1); fr.addResult(featureRuntime.setupResult); @@ -116,7 +115,7 @@ private Map initMagicVariables() { // and not "visible" and tracked in ScenarioEngine.vars // one consequence is that they won't show up in the debug variables view // but more importantly don't get passed back to caller and float around, bloating memory - Map map = new HashMap(); + Map map = new HashMap<>(); if (!caller.isNone()) { // karate principle: parent variables are always "visible" // so we inject the parent variables @@ -172,7 +171,7 @@ private Embed saveToFileAndCreateEmbed(byte[] bytes, ResourceType resourceType) public Embed embed(byte[] bytes, ResourceType resourceType) { if (embeds == null) { - embeds = new ArrayList(); + embeds = new ArrayList<>(); } Embed embed = saveToFileAndCreateEmbed(bytes, resourceType); embeds.add(embed); @@ -190,7 +189,7 @@ public Embed embedVideo(File file) { public void addCallResult(FeatureResult fr) { if (callResults == null) { - callResults = new ArrayList(); + callResults = new ArrayList<>(); } callResults.add(fr); } @@ -269,7 +268,7 @@ public boolean hotReload() { } public Map getScenarioInfo() { - Map info = new HashMap(5); + Map info = new HashMap<>(5); File featureFile = featureRuntime.featureCall.feature.getResource().getFile(); if (featureFile != null) { info.put("featureDir", featureFile.getParent()); diff --git a/karate-core/src/test/java/com/intuit/karate/core/runner/RunnerTest.java b/karate-core/src/test/java/com/intuit/karate/core/runner/RunnerTest.java index 5534b8da7..164d6f3c4 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/runner/RunnerTest.java +++ b/karate-core/src/test/java/com/intuit/karate/core/runner/RunnerTest.java @@ -74,6 +74,18 @@ void testRunningFeatureFromJavaApi() { assertEquals("normal", result.get("configSource")); } + @Test + void testRunningFeatureWithFailAnnotationFromJavaApi() { + Results results = Runner.path("classpath:com/intuit/karate/core/fail-tag.feature").parallel(1); + assertEquals(0, results.getFailCount()); + } + + @Test + void testRunningFeatureWithFailAnnotationFailureFromJavaApi() { + Results results = Runner.path("classpath:com/intuit/karate/core/fail-tag-failure.feature").parallel(1); + assertEquals(1, results.getFailCount()); + } + @Test void testRunningFeatureFailureFromJavaApi() { try {