Skip to content

Commit

Permalink
karatelabs#2641 introduce @fail tag to mark tests which are expected …
Browse files Browse the repository at this point in the history
…to fail
  • Loading branch information
dve committed Jan 14, 2025
1 parent 459aac2 commit c248a89
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
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 c248a89

Please sign in to comment.