Skip to content

Commit

Permalink
Merge pull request #2643 from Fork-World/2641-fail-tag
Browse files Browse the repository at this point in the history
#2641 introduce @fail tag to mark tests which are expected to fail
  • Loading branch information
ptrthomas authored Jan 16, 2025
2 parents e879249 + 334b893 commit f06578b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4328,6 +4328,7 @@ For completeness, the "built-in" tags are the following:
Tag | Description
--- | -----------
`@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
`@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
`@parallel` | See [`@parallel=false`](#parallelfalse)
`@report` | See [`@report=false`](#reportfalse)
`@setup` | See [`@setup`](#setup)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
*/
public class ScenarioResult implements Comparable<ScenarioResult> {

private final List<StepResult> stepResults = new ArrayList();
private final List<StepResult> stepResults = new ArrayList<>();
private final Scenario scenario;

private StepResult failedStep;
Expand Down Expand Up @@ -363,4 +363,7 @@ public String toString() {
return failedStep == null ? scenario.toString() : failedStep + "";
}

public void ignoreFailedStep() {
failedStep = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
1 change: 1 addition & 0 deletions karate-core/src/main/java/com/intuit/karate/core/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Feature: fail tag failure

@fail
Scenario:
* def a = 1 + 2
* match a == 3
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Feature: fail tag

@fail
Scenario:
* def a = 1 + 2
* match a == 4

0 comments on commit f06578b

Please sign in to comment.