-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
junit5: Add support for
--test_runner_fail_fast
When the Bazel flag `--test_runner_fail_fast` is enabled, the JUnit 5 runner will now skip all other tests after the first one has failed in an attempt to speed up the (failing) test run. This is useful in some CI setups as well as during bisection.
- Loading branch information
Showing
5 changed files
with
61 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5/FailFastExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.github.bazel_contrib.contrib_rules_jvm.junit5; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import org.junit.jupiter.api.extension.ConditionEvaluationResult; | ||
import org.junit.jupiter.api.extension.ExecutionCondition; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.platform.engine.TestExecutionResult; | ||
import org.junit.platform.engine.TestExecutionResult.Status; | ||
import org.junit.platform.launcher.TestExecutionListener; | ||
import org.junit.platform.launcher.TestIdentifier; | ||
|
||
public class FailFastExtension implements ExecutionCondition, TestExecutionListener { | ||
/** | ||
* This environment variable is set to 1 if Bazel is run with --test_runner_fail_fast, indicating | ||
* that the test runner should exit as soon as possible after the first failure. {@see | ||
* https://github.com/bazelbuild/bazel/commit/957554037ced26dc1860b9c23445a8ccc44f697e} | ||
*/ | ||
private static final boolean SHOULD_FAIL_FAST = | ||
"1".equals(System.getenv("TESTBRIDGE_TEST_RUNNER_FAIL_FAST")); | ||
|
||
private static final AtomicBoolean SOME_TEST_FAILED = new AtomicBoolean(); | ||
|
||
@Override | ||
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext extensionContext) { | ||
if (!SHOULD_FAIL_FAST) { | ||
return ConditionEvaluationResult.enabled( | ||
"Running test since --test_runner_fail_fast is not enabled"); | ||
} | ||
if (SOME_TEST_FAILED.get()) { | ||
return ConditionEvaluationResult.disabled( | ||
"Skipping test since --test_runner_fail_fast is enabled and another test has failed"); | ||
} else { | ||
return ConditionEvaluationResult.enabled("Running test since no other test has failed yet"); | ||
} | ||
} | ||
|
||
@Override | ||
public void executionFinished( | ||
TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { | ||
if (SHOULD_FAIL_FAST && testExecutionResult.getStatus().equals(Status.FAILED)) { | ||
SOME_TEST_FAILED.set(true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...trib/contrib_rules_jvm/junit5/META-INF/services/org.junit.jupiter.api.extension.Extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.github.bazel_contrib.contrib_rules_jvm.junit5.FailFastExtension |