-
Notifications
You must be signed in to change notification settings - Fork 24
WIP Enable filtering of killing test units #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dmitry-timofeev
wants to merge
2
commits into
pitest:master
Choose a base branch
from
dmitry-timofeev:enable-killing-unit-filtering
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
48 changes: 48 additions & 0 deletions
48
src/main/java/org/pitest/junit5/ConditionalTestFilter.java
This file contains hidden or 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,48 @@ | ||
| package org.pitest.junit5; | ||
|
|
||
| import org.junit.jupiter.api.extension.ConditionEvaluationResult; | ||
| import org.junit.jupiter.api.extension.ExecutionCondition; | ||
| import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
|
||
| /** | ||
| * A test filter accepting all tests until requested to reject | ||
| * all subsequent tests through an {@linkplain TestUnitExecutionsRegistry executions registry}. | ||
| */ | ||
| public class ConditionalTestFilter implements ExecutionCondition { | ||
|
|
||
| static final String EXECUTION_ID_KEY = "org.pitest.junit5.executionId"; | ||
|
|
||
| private static final ConditionEvaluationResult DISABLED = ConditionEvaluationResult.disabled( | ||
| "Pitest has requested to skip remaining tests in a test unit"); | ||
| private static final ConditionEvaluationResult ENABLED = ConditionEvaluationResult.enabled(null); | ||
|
|
||
| private final TestUnitExecutionsRegistry executionsRegistry; | ||
|
|
||
| @SuppressWarnings("unused") // Used through reflection by the ServiceLoader | ||
| // when this extension is requested by JUnit Jupiter Engine | ||
| public ConditionalTestFilter() { | ||
| this(TestUnitExecutionsRegistry.getInstance()); | ||
| } | ||
|
|
||
| // Visible for testing to be able to inject executionsRegistry | ||
| ConditionalTestFilter(TestUnitExecutionsRegistry executionsRegistry) { | ||
| this.executionsRegistry = executionsRegistry; | ||
| } | ||
|
|
||
| @Override | ||
| public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { | ||
| boolean shallReject = context.getConfigurationParameter(EXECUTION_ID_KEY) | ||
| .map(this::shallRejectTests) | ||
| .orElse(false); | ||
|
|
||
| if (shallReject) { | ||
| return DISABLED; | ||
| } else { | ||
| return ENABLED; | ||
| } | ||
| } | ||
|
|
||
| private boolean shallRejectTests(String executionId) { | ||
| return executionsRegistry.getStatus(executionId) == TestUnitExecutionsRegistry.Status.ABORT; | ||
| } | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,98 +1,127 @@ | ||
| /* | ||
| * Copyright 2017 Tobias Stadler | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and limitations under the License. | ||
| */ | ||
| package org.pitest.junit5; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import org.junit.platform.engine.TestExecutionResult; | ||
| import org.junit.platform.engine.discovery.DiscoverySelectors; | ||
| import org.junit.platform.engine.support.descriptor.MethodSource; | ||
| import org.junit.platform.launcher.Launcher; | ||
| import org.junit.platform.launcher.LauncherDiscoveryRequest; | ||
| import org.junit.platform.launcher.TestExecutionListener; | ||
| import org.junit.platform.launcher.TestIdentifier; | ||
| import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; | ||
| import org.junit.platform.launcher.core.LauncherFactory; | ||
| import org.pitest.testapi.AbstractTestUnit; | ||
| import org.pitest.testapi.Description; | ||
| import org.pitest.testapi.ResultCollector; | ||
|
|
||
| /** | ||
| * | ||
| * @author Tobias Stadler | ||
| */ | ||
| public class JUnit5TestUnit extends AbstractTestUnit { | ||
|
|
||
| private final Class<?> testClass; | ||
|
|
||
| private final TestIdentifier testIdentifier; | ||
|
|
||
| public JUnit5TestUnit(Class<?> testClass, TestIdentifier testIdentifier) { | ||
| super(new Description(testIdentifier.getDisplayName(), testClass)); | ||
| this.testClass = testClass; | ||
| this.testIdentifier = testIdentifier; | ||
| } | ||
|
|
||
| @Override | ||
| public void execute(ResultCollector resultCollector) { | ||
| Launcher launcher = LauncherFactory.create(); | ||
| LauncherDiscoveryRequest launcherDiscoveryRequest = LauncherDiscoveryRequestBuilder | ||
| .request() | ||
| .selectors(DiscoverySelectors.selectUniqueId(testIdentifier.getUniqueId())) | ||
| .build(); | ||
|
|
||
| launcher.registerTestExecutionListeners(new TestExecutionListener() { | ||
| @Override | ||
| public void executionSkipped(TestIdentifier testIdentifier, String reason) { | ||
| testIdentifier.getSource().ifPresent(testSource -> { | ||
| if (testSource instanceof MethodSource) { | ||
| resultCollector.notifySkipped(new Description(testIdentifier.getDisplayName(), testClass)); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void executionStarted(TestIdentifier testIdentifier) { | ||
| testIdentifier.getSource().ifPresent(testSource -> { | ||
| if (testSource instanceof MethodSource) { | ||
| resultCollector.notifyStart(new Description(testIdentifier.getDisplayName(), testClass)); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { | ||
| testIdentifier.getSource().ifPresent(testSource -> { | ||
| if (testSource instanceof MethodSource) { | ||
| Optional<Throwable> throwable = testExecutionResult.getThrowable(); | ||
|
|
||
| if (TestExecutionResult.Status.ABORTED == testExecutionResult.getStatus()) { | ||
| // abort treated as success | ||
| // see: https://junit.org/junit5/docs/5.0.0/api/org/junit/jupiter/api/Assumptions.html | ||
| resultCollector.notifyEnd(new Description(testIdentifier.getDisplayName(), testClass)); | ||
| } else if (throwable.isPresent()) { | ||
| resultCollector.notifyEnd(new Description(testIdentifier.getDisplayName(), testClass), throwable.get()); | ||
| } else { | ||
| resultCollector.notifyEnd(new Description(testIdentifier.getDisplayName(), testClass)); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| }); | ||
| launcher.execute(launcherDiscoveryRequest); | ||
| } | ||
|
|
||
| } | ||
| /* | ||
| * Copyright 2017 Tobias Stadler | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and limitations under the License. | ||
| */ | ||
| package org.pitest.junit5; | ||
|
|
||
| import org.junit.platform.engine.TestExecutionResult; | ||
| import org.junit.platform.engine.discovery.DiscoverySelectors; | ||
| import org.junit.platform.engine.support.descriptor.MethodSource; | ||
| import org.junit.platform.launcher.Launcher; | ||
| import org.junit.platform.launcher.LauncherDiscoveryRequest; | ||
| import org.junit.platform.launcher.TestExecutionListener; | ||
| import org.junit.platform.launcher.TestIdentifier; | ||
| import org.junit.platform.launcher.TestPlan; | ||
| import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; | ||
| import org.junit.platform.launcher.core.LauncherFactory; | ||
| import org.pitest.testapi.AbstractTestUnit; | ||
| import org.pitest.testapi.Description; | ||
| import org.pitest.testapi.ResultCollector; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * @author Tobias Stadler | ||
| */ | ||
| public class JUnit5TestUnit extends AbstractTestUnit { | ||
|
|
||
| private final Class<?> testClass; | ||
|
|
||
| private final TestIdentifier testIdentifier; | ||
|
|
||
| public JUnit5TestUnit(Class<?> testClass, TestIdentifier testIdentifier) { | ||
| super(new Description(testIdentifier.getDisplayName(), testClass)); | ||
| this.testClass = testClass; | ||
| this.testIdentifier = testIdentifier; | ||
| } | ||
|
|
||
| @Override | ||
| public void execute(ResultCollector resultCollector) { | ||
| Launcher launcher = LauncherFactory.create(); | ||
|
|
||
| String executionId = getExecutionId(resultCollector); | ||
| LauncherDiscoveryRequest launcherDiscoveryRequest = LauncherDiscoveryRequestBuilder | ||
| .request() | ||
| .configurationParameter("junit.jupiter.extensions.autodetection.enabled", "true") | ||
| .configurationParameter(ConditionalTestFilter.EXECUTION_ID_KEY, executionId) | ||
| .selectors(DiscoverySelectors.selectUniqueId(testIdentifier.getUniqueId())) | ||
| .build(); | ||
|
|
||
| // todo: shall we inject the registry? | ||
| TestUnitExecutionsRegistry executionsRegistry = TestUnitExecutionsRegistry.getInstance(); | ||
| launcher.registerTestExecutionListeners(new TestExecutionListener() { | ||
| @Override | ||
| public void testPlanExecutionStarted(TestPlan testPlan) { | ||
| executionsRegistry.add(executionId); | ||
| } | ||
|
|
||
| @Override | ||
| public void testPlanExecutionFinished(TestPlan testPlan) { | ||
| executionsRegistry.remove(executionId); | ||
| } | ||
|
|
||
| @Override | ||
| public void executionSkipped(TestIdentifier testIdentifier, String reason) { | ||
| testIdentifier.getSource().ifPresent(testSource -> { | ||
| if (testSource instanceof MethodSource) { | ||
| resultCollector | ||
| .notifySkipped(new Description(testIdentifier.getDisplayName(), testClass)); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void executionStarted(TestIdentifier testIdentifier) { | ||
| testIdentifier.getSource().ifPresent(testSource -> { | ||
| if (testSource instanceof MethodSource) { | ||
| resultCollector | ||
| .notifyStart(new Description(testIdentifier.getDisplayName(), testClass)); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void executionFinished(TestIdentifier testIdentifier, | ||
| TestExecutionResult testExecutionResult) { | ||
| testIdentifier.getSource().ifPresent(testSource -> { | ||
| if (testSource instanceof MethodSource) { | ||
| Optional<Throwable> throwable = testExecutionResult.getThrowable(); | ||
|
|
||
| if (TestExecutionResult.Status.ABORTED == testExecutionResult.getStatus()) { | ||
| // abort treated as success | ||
| // see: https://junit.org/junit5/docs/5.0.0/api/org/junit/jupiter/api/Assumptions.html | ||
| resultCollector.notifyEnd(new Description(testIdentifier.getDisplayName(), testClass)); | ||
| } else if (throwable.isPresent()) { | ||
| resultCollector.notifyEnd(new Description(testIdentifier.getDisplayName(), testClass), | ||
| throwable.get()); | ||
|
|
||
| if (resultCollector.shouldExit()) { | ||
| executionsRegistry.abortExecution(executionId); | ||
| } | ||
| } else { | ||
| resultCollector | ||
| .notifyEnd(new Description(testIdentifier.getDisplayName(), testClass)); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| }); | ||
| launcher.execute(launcherDiscoveryRequest); | ||
| } | ||
|
|
||
| private String getExecutionId(ResultCollector resultCollector) { | ||
| return testIdentifier.getUniqueId() + ":" | ||
| + System.identityHashCode(resultCollector); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not entirely happy with this global registry, but haven't found another way to inject an object into the extension (each created launcher instantiates an engine which, in turn, creates a new extension — one cannot supply an extension through launcher, because it is not aware of engine-specific extensions).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a better solution, but I'd be very reluctant to merge in anything that introduced global state.
Just had a scan of the junit 5 apis and the "smaller test unit" approach might be easier for junit 5 - assuming that parameterized tests etc are identifiable with their own ids during the scan stage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess that they can only be identified if they are executed, because the framework cannot possibly determine how many items a method source will return (or how many dynamic tests a
@TestFactorywill generate). Another questions to consider:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only way to know if the performance impact is significant would be to run some experiments, but as I think you are probably correct that the individual ids will not be discoverable at the scan stage the point is moot.
Which takes us back to trying to break out of the running test again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some benchmarks of the various options are essential indeed.
There is a related discussion regarding APIs to terminate dynamic (a subset of test containers) tests early, where I tried to summarize the issue with the approach in the present prototype: junit-team/junit-framework#431