Skip to content

Commit f06578b

Browse files
authored
Merge pull request #2643 from Fork-World/2641-fail-tag
#2641 introduce @fail tag to mark tests which are expected to fail
2 parents e879249 + 334b893 commit f06578b

File tree

7 files changed

+40
-1
lines changed

7 files changed

+40
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -4328,6 +4328,7 @@ For completeness, the "built-in" tags are the following:
43284328
Tag | Description
43294329
--- | -----------
43304330
`@ignore` | Any `Scenario` with (or that has inherited) this tag will be skipped at run-time. This does not apply to anything that is "called" though
4331+
`@fail` | Any `Scenario` with (or that has inherited) this tag will be expected to fail. This can be used if e.g. tests are written before fixes
43314332
`@parallel` | See [`@parallel=false`](#parallelfalse)
43324333
`@report` | See [`@report=false`](#reportfalse)
43334334
`@setup` | See [`@setup`](#setup)

karate-core/src/main/java/com/intuit/karate/core/ScenarioResult.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
*/
4040
public class ScenarioResult implements Comparable<ScenarioResult> {
4141

42-
private final List<StepResult> stepResults = new ArrayList();
42+
private final List<StepResult> stepResults = new ArrayList<>();
4343
private final Scenario scenario;
4444

4545
private StepResult failedStep;
@@ -363,4 +363,7 @@ public String toString() {
363363
return failedStep == null ? scenario.toString() : failedStep + "";
364364
}
365365

366+
public void ignoreFailedStep() {
367+
failedStep = null;
368+
}
366369
}

karate-core/src/main/java/com/intuit/karate/core/ScenarioRuntime.java

+10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
*/
4747
public class ScenarioRuntime implements Runnable {
4848

49+
public static final String EXPECT_TEST_TO_FAIL_BECAUSE_OF_FAIL_TAG = "Expect test to fail because of @fail tag";
4950
public final Logger logger;
5051
public final FeatureRuntime featureRuntime;
5152
public final ScenarioCall caller;
@@ -510,6 +511,15 @@ public void afterRun() {
510511
engine.stop(currentStepResult);
511512
}
512513
addStepLogEmbedsAndCallResults();
514+
if (tags.contains(Tag.FAIL)) {
515+
if (result.isFailed()) {
516+
result.ignoreFailedStep();
517+
result.addFakeStepResult(EXPECT_TEST_TO_FAIL_BECAUSE_OF_FAIL_TAG, null);
518+
} else {
519+
result.addFakeStepResult(EXPECT_TEST_TO_FAIL_BECAUSE_OF_FAIL_TAG,
520+
new Throwable(EXPECT_TEST_TO_FAIL_BECAUSE_OF_FAIL_TAG));
521+
}
522+
}
513523
} catch (Exception e) {
514524
error = e;
515525
logError("scenario [cleanup] failed\n" + e.getMessage());

karate-core/src/main/java/com/intuit/karate/core/Tag.java

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class Tag {
3737
public static final String ENV = "env";
3838
public static final String ENVNOT = "envnot";
3939
public static final String SETUP = "setup";
40+
public static final String FAIL = "fail";
4041

4142
private final int line;
4243
private final String text;

karate-core/src/test/java/com/intuit/karate/core/FeatureRuntimeTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ private void matchContains(Object actual, Object expected) {
6060
assertTrue(mr.pass, mr.message);
6161
}
6262

63+
@Test
64+
void testFailTag() {
65+
fail = false;
66+
run("fail-tag.feature");
67+
}
68+
69+
@Test
70+
void testFailTagFailure() {
71+
fail = true;
72+
run("fail-tag-failure.feature");
73+
}
74+
6375
@Test
6476
void testFail1() {
6577
fail = true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Feature: fail tag failure
2+
3+
@fail
4+
Scenario:
5+
* def a = 1 + 2
6+
* match a == 3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Feature: fail tag
2+
3+
@fail
4+
Scenario:
5+
* def a = 1 + 2
6+
* match a == 4

0 commit comments

Comments
 (0)