-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prior to this commit only a flat collection of dynamically generated tests was allowed as the return type of a @testfactory annotated method. The collection entry type was set to `DynamicTest`. This commit introduces the abstract base class `DynamicNode` and a `DynamicContainer` class collecting node instance. @testfactory annotated methods are now allowed to return a collection of nodes -- which can be a test or a named container of tests. Creating a dynamic tree of tests like that gives users the ability to better structure the generated tests. The old behaviour, returning only instances of `DynamicTest`, is preserved.
- Loading branch information
Showing
7 changed files
with
217 additions
and
34 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
51 changes: 51 additions & 0 deletions
51
junit-jupiter-api/src/main/java/org/junit/jupiter/api/DynamicContainer.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,51 @@ | ||
/* | ||
* Copyright 2015-2017 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.jupiter.api; | ||
|
||
import static org.junit.platform.commons.meta.API.Usage.Experimental; | ||
|
||
import java.util.List; | ||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
|
||
import org.junit.platform.commons.meta.API; | ||
import org.junit.platform.commons.util.CollectionUtils; | ||
import org.junit.platform.commons.util.Preconditions; | ||
|
||
/** | ||
* A {@code DynamicContainer} is a container generated at runtime. | ||
* | ||
* @since 5.0 | ||
*/ | ||
@API(Experimental) | ||
public class DynamicContainer extends DynamicNode { | ||
|
||
public static DynamicContainer dynamicContainer(String displayName, Iterable<? extends DynamicNode> dynamicNodes) { | ||
return new DynamicContainer(displayName, StreamSupport.stream(dynamicNodes.spliterator(), false)); | ||
} | ||
|
||
public static DynamicContainer dynamicContainer(String displayName, Stream<? extends DynamicNode> dynamicNodes) { | ||
return new DynamicContainer(displayName, dynamicNodes); | ||
} | ||
|
||
private final List<DynamicNode> dynamicNodes; | ||
|
||
private DynamicContainer(String displayName, Stream<? extends DynamicNode> dynamicNodes) { | ||
super(displayName); | ||
Preconditions.notNull(dynamicNodes, "dynamicNodes must not be null"); | ||
this.dynamicNodes = dynamicNodes.collect(CollectionUtils.toUnmodifiableList()); | ||
Preconditions.containsNoNullElements(this.dynamicNodes, "individual dynamic node must not be null"); | ||
} | ||
|
||
public Iterable<DynamicNode> getDynamicNodes() { | ||
return dynamicNodes; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
junit-jupiter-api/src/main/java/org/junit/jupiter/api/DynamicNode.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,46 @@ | ||
/* | ||
* Copyright 2015-2017 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.jupiter.api; | ||
|
||
import static org.junit.platform.commons.meta.API.Usage.Experimental; | ||
|
||
import org.junit.platform.commons.meta.API; | ||
import org.junit.platform.commons.util.Preconditions; | ||
import org.junit.platform.commons.util.ToStringBuilder; | ||
|
||
/** | ||
* A {@code DynamicNode} is the abstract basis class for a container or a test | ||
* case generated at runtime. | ||
* | ||
* @since 5.0 | ||
*/ | ||
@API(Experimental) | ||
public abstract class DynamicNode { | ||
|
||
private final String displayName; | ||
|
||
DynamicNode(String displayName) { | ||
this.displayName = Preconditions.notBlank(displayName, "displayName must not be null or blank"); | ||
} | ||
|
||
/** | ||
* Get the display name of this {@code DynamicTest}. | ||
*/ | ||
public String getDisplayName() { | ||
return this.displayName; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).append("displayName", displayName).toString(); | ||
} | ||
|
||
} |
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
58 changes: 58 additions & 0 deletions
58
...ine/src/main/java/org/junit/jupiter/engine/descriptor/DynamicContainerTestDescriptor.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,58 @@ | ||
/* | ||
* Copyright 2015-2017 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.jupiter.engine.descriptor; | ||
|
||
import static org.junit.jupiter.engine.descriptor.TestFactoryTestDescriptor.createDynamicDescriptor; | ||
|
||
import org.junit.jupiter.api.DynamicContainer; | ||
import org.junit.jupiter.api.DynamicNode; | ||
import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext; | ||
import org.junit.platform.engine.TestDescriptor; | ||
import org.junit.platform.engine.TestSource; | ||
import org.junit.platform.engine.UniqueId; | ||
|
||
/** | ||
* {@link TestDescriptor} for a {@link DynamicContainer}. | ||
* | ||
* @since 5.0 | ||
*/ | ||
class DynamicContainerTestDescriptor extends JupiterTestDescriptor { | ||
|
||
private final DynamicContainer dynamicContainer; | ||
private final TestSource testSource; | ||
|
||
DynamicContainerTestDescriptor(UniqueId uniqueId, DynamicContainer dynamicContainer, TestSource testSource) { | ||
super(uniqueId, dynamicContainer.getDisplayName()); | ||
this.dynamicContainer = dynamicContainer; | ||
this.testSource = testSource; | ||
setSource(testSource); | ||
} | ||
|
||
@Override | ||
public Type getType() { | ||
return Type.CONTAINER; | ||
} | ||
|
||
@Override | ||
public boolean isLeaf() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext context, | ||
DynamicTestExecutor dynamicTestExecutor) throws Exception { | ||
int index = 1; | ||
for (DynamicNode childNode : dynamicContainer.getDynamicNodes()) { | ||
dynamicTestExecutor.execute(createDynamicDescriptor(this, childNode, index++, testSource)); | ||
} | ||
return context; | ||
} | ||
} |
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
1a57e4f
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.
Very nicely done! 👍