Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<assertj.version>3.11.1</assertj.version>
<junit.platform.version>1.3.1</junit.platform.version>
<junit.version>5.3.1</junit.version>
<mockito.version>2.7.6</mockito.version>
<mockito.version>2.23.0</mockito.version>
<pitest.version>1.4.3</pitest.version>
</properties>

Expand Down Expand Up @@ -106,14 +106,27 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<!-- Test dependencies: -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/org/pitest/junit5/ConditionalTestFilter.java
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());
Copy link
Contributor Author

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).

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assuming that parameterized tests etc are identifiable with their own ids during the scan stage.

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 @TestFactory will generate). Another questions to consider:

  1. If we run parameterized/dynamic tests one-by-one, won't that cause too much overhead? In this case, for example, for N executions you will have to call the source method N times (and its implementation will likely instantiate a whole collection of parameters), whereas when you tell JUnit to execute a test container, it will call the source method only once.
  2. If the source method/test factory are not deterministic, I'm not sure we can reliably identify them.

Copy link
Contributor

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.

Copy link
Contributor Author

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

}

// 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;
}
}
225 changes: 127 additions & 98 deletions src/main/java/org/pitest/junit5/JUnit5TestUnit.java
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);
}
}
11 changes: 6 additions & 5 deletions src/main/java/org/pitest/junit5/JUnit5TestUnitFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
*/
package org.pitest.junit5;

import java.util.List;
import java.util.Set;

import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.engine.support.descriptor.MethodSource;
import org.junit.platform.launcher.Launcher;
Expand All @@ -28,6 +23,12 @@
import org.pitest.testapi.TestUnit;
import org.pitest.testapi.TestUnitFinder;

import java.util.List;
import java.util.Set;

import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;

/**
*
* @author Tobias Stadler
Expand Down
Loading