From c248a895c9d11895b709271850de7773558c55e0 Mon Sep 17 00:00:00 2001 From: Daniel Vergien Date: Tue, 14 Jan 2025 16:12:00 +0100 Subject: [PATCH] #2641 introduce @fail tag to mark tests which are expected to fail --- .../java/com/intuit/karate/core/ScenarioResult.java | 5 ++++- .../java/com/intuit/karate/core/ScenarioRuntime.java | 10 ++++++++++ .../src/main/java/com/intuit/karate/core/Tag.java | 1 + .../com/intuit/karate/core/FeatureRuntimeTest.java | 12 ++++++++++++ .../com/intuit/karate/core/fail-tag-failure.feature | 6 ++++++ .../java/com/intuit/karate/core/fail-tag.feature | 6 ++++++ 6 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 karate-core/src/test/java/com/intuit/karate/core/fail-tag-failure.feature create mode 100644 karate-core/src/test/java/com/intuit/karate/core/fail-tag.feature 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 1a85043c4..384f6c8d4 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 @@ -39,7 +39,7 @@ */ public class ScenarioResult implements Comparable { - private final List stepResults = new ArrayList(); + private final List stepResults = new ArrayList<>(); private final Scenario scenario; private StepResult failedStep; @@ -363,4 +363,7 @@ public String toString() { return failedStep == null ? scenario.toString() : failedStep + ""; } + public void ignoreFailedStep() { + failedStep = null; + } } 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 096a6f330..76fee3fdb 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 @@ -46,6 +46,7 @@ */ public class ScenarioRuntime implements Runnable { + public static final String EXPECT_TEST_TO_FAIL_BECAUSE_OF_FAIL_TAG = "Expect test to fail because of @fail tag"; public final Logger logger; public final FeatureRuntime featureRuntime; public final ScenarioCall caller; @@ -510,6 +511,15 @@ public void afterRun() { engine.stop(currentStepResult); } addStepLogEmbedsAndCallResults(); + if (tags.contains(Tag.FAIL)) { + if (result.isFailed()) { + result.ignoreFailedStep(); + result.addFakeStepResult(EXPECT_TEST_TO_FAIL_BECAUSE_OF_FAIL_TAG, null); + } else { + result.addFakeStepResult(EXPECT_TEST_TO_FAIL_BECAUSE_OF_FAIL_TAG, + new Throwable(EXPECT_TEST_TO_FAIL_BECAUSE_OF_FAIL_TAG)); + } + } } catch (Exception e) { error = e; logError("scenario [cleanup] failed\n" + e.getMessage()); diff --git a/karate-core/src/main/java/com/intuit/karate/core/Tag.java b/karate-core/src/main/java/com/intuit/karate/core/Tag.java index d1ae36b81..d50508ec1 100644 --- a/karate-core/src/main/java/com/intuit/karate/core/Tag.java +++ b/karate-core/src/main/java/com/intuit/karate/core/Tag.java @@ -37,6 +37,7 @@ public class Tag { public static final String ENV = "env"; public static final String ENVNOT = "envnot"; public static final String SETUP = "setup"; + public static final String FAIL = "fail"; private final int line; private final String text; diff --git a/karate-core/src/test/java/com/intuit/karate/core/FeatureRuntimeTest.java b/karate-core/src/test/java/com/intuit/karate/core/FeatureRuntimeTest.java index 5a3b04fa2..58929e60a 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/FeatureRuntimeTest.java +++ b/karate-core/src/test/java/com/intuit/karate/core/FeatureRuntimeTest.java @@ -60,6 +60,18 @@ private void matchContains(Object actual, Object expected) { assertTrue(mr.pass, mr.message); } + @Test + void testFailTag() { + fail = false; + run("fail-tag.feature"); + } + + @Test + void testFailTagFailure() { + fail = true; + run("fail-tag-failure.feature"); + } + @Test void testFail1() { fail = true; diff --git a/karate-core/src/test/java/com/intuit/karate/core/fail-tag-failure.feature b/karate-core/src/test/java/com/intuit/karate/core/fail-tag-failure.feature new file mode 100644 index 000000000..b090cc34d --- /dev/null +++ b/karate-core/src/test/java/com/intuit/karate/core/fail-tag-failure.feature @@ -0,0 +1,6 @@ +Feature: fail tag failure + +@fail +Scenario: +* def a = 1 + 2 +* match a == 3 diff --git a/karate-core/src/test/java/com/intuit/karate/core/fail-tag.feature b/karate-core/src/test/java/com/intuit/karate/core/fail-tag.feature new file mode 100644 index 000000000..0dbc3e24b --- /dev/null +++ b/karate-core/src/test/java/com/intuit/karate/core/fail-tag.feature @@ -0,0 +1,6 @@ +Feature: fail tag + +@fail +Scenario: +* def a = 1 + 2 +* match a == 4