Skip to content
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

Invoke lifecycle hooks around each dynamic test #735

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.List;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Consumer;

import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
Expand Down Expand Up @@ -108,6 +109,12 @@ public SkipResult shouldBeSkipped(JupiterEngineExecutionContext context) throws
@Override
public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext context,
DynamicTestExecutor dynamicTestExecutor) throws Exception {
executeTestBetweenBeforeAndAfterHooks(context, c -> invokeTestMethod(c, dynamicTestExecutor));
return context;
}

protected void executeTestBetweenBeforeAndAfterHooks(JupiterEngineExecutionContext context,
Consumer<JupiterEngineExecutionContext> testExecutor) {
ThrowableCollector throwableCollector = context.getThrowableCollector();

// @formatter:off
Expand All @@ -117,7 +124,7 @@ public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext conte
if (throwableCollector.isEmpty()) {
invokeBeforeTestExecutionCallbacks(context);
if (throwableCollector.isEmpty()) {
invokeTestMethod(context, dynamicTestExecutor);
testExecutor.accept(context);
}
invokeAfterTestExecutionCallbacks(context);
}
Expand All @@ -127,8 +134,6 @@ public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext conte
// @formatter:on

throwableCollector.assertEmpty();

return context;
}

private void invokeBeforeEachCallbacks(JupiterEngineExecutionContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.junit.jupiter.api.extension.TestExtensionContext;
import org.junit.jupiter.engine.execution.ExecutableInvoker;
import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext;
import org.junit.jupiter.engine.execution.ThrowableCollector;
import org.junit.platform.commons.JUnitException;
import org.junit.platform.commons.meta.API;
import org.junit.platform.commons.util.CollectionUtils;
Expand Down Expand Up @@ -68,6 +69,18 @@ public boolean isLeaf() {
return true;
}

@Override
public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext context,
Copy link
Member

Choose a reason for hiding this comment

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

Please explain in great detail the need to override the execute() method, not only here but also in the commit message.

Copy link
Contributor Author

@ledoyen ledoyen Mar 15, 2017

Choose a reason for hiding this comment

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

I choose to override the execute() method here to prevent calls to before and after hooks around a container execution.
This is because a hook implementation can not know on which (of a container or a test) it is executed.
I assumed that @BeforeEach meant before each test, in this particular case, unlike what's said in the annotations javadoc.

A cleaner way can be to declare a new annotation @BeforeEachTest and let callback implementations know about the type of test (container or not).

If this is something you prefer, I can give it a try.

Copy link
Member

Choose a reason for hiding this comment

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

Unless I'm mistaken, lifecycle callbacks currently apply to @TestFactory methods.

So it appears that you have removed this functionality, which some people may depend on.

Copy link
Member

Choose a reason for hiding this comment

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

At the moment, no, I don't think we want to introduce any additional lifecycle annotations.

Copy link
Contributor Author

@ledoyen ledoyen Mar 16, 2017

Choose a reason for hiding this comment

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

What do you think about invoking lifecycle callbacks around @TestFactory methods and around each dynamic tests and making implementations aware of the type (container or test) they are invoked on ?
This could be done by adding an isContainer:boolean method to the TestExtensionContext like on the TestDescriptor interface.

I tried to improve the MockitoExtension by adding a reset of mocked field / parameters in Store before each test.
This is why I removed callback invocation around @TestFactory methods in favor of dynamic tests in the first place.

DynamicTestExecutor dynamicTestExecutor) throws Exception {
ThrowableCollector throwableCollector = context.getThrowableCollector();

invokeTestMethod(context, dynamicTestExecutor);

throwableCollector.assertEmpty();

return context;
}

@Override
protected void invokeTestMethod(JupiterEngineExecutionContext context, DynamicTestExecutor dynamicTestExecutor) {
TestExtensionContext testExtensionContext = (TestExtensionContext) context.getExtensionContext();
Expand All @@ -80,8 +93,8 @@ protected void invokeTestMethod(JupiterEngineExecutionContext context, DynamicTe
try (Stream<DynamicTest> dynamicTestStream = toDynamicTestStream(testExtensionContext,
testFactoryMethodResult)) {
AtomicInteger index = new AtomicInteger();
dynamicTestStream.forEach(
dynamicTest -> registerAndExecute(dynamicTest, index.incrementAndGet(), dynamicTestExecutor));
dynamicTestStream.forEach(dynamicTest -> executeTestBetweenBeforeAndAfterHooks(context,
c -> registerAndExecute(dynamicTest, index.incrementAndGet(), dynamicTestExecutor)));
}
catch (ClassCastException ex) {
throw invalidReturnTypeException(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@
import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -151,6 +155,38 @@ public void beforeEachAndAfterEachCallbacksDeclaredOnInterfaceAndClass() {
// @formatter:on
}

@Test
public void beforeEachAndAfterEachCallbacksOnDynamicTests() {
LauncherDiscoveryRequest request = request().selectors(selectClass(DynamicTestTestCase.class)).build();

ExecutionEventRecorder eventRecorder = executeTests(request);

assertEquals(2, eventRecorder.getTestStartedCount(), "# tests started");
assertEquals(2, eventRecorder.getTestSuccessfulCount(), "# tests succeeded");
assertEquals(0, eventRecorder.getTestSkippedCount(), "# tests skipped");
assertEquals(0, eventRecorder.getTestAbortedCount(), "# tests aborted");
assertEquals(0, eventRecorder.getTestFailedCount(), "# tests failed");

// @formatter:off
assertEquals(asList(
// first dynamic test
"fooBeforeEachCallback",
"beforeEachMethod",
"localTestMethod1",
"afterEachMethod",
"fooAfterEachCallback",

// second dynamic test
"fooBeforeEachCallback",
"beforeEachMethod",
"localTestMethod2",
"afterEachMethod",
"fooAfterEachCallback"

), callSequence, "wrong call sequence");
// @formatter:on
}

@Test
public void beforeEachCallbackThrowsAnException() {
LauncherDiscoveryRequest request = request().selectors(
Expand Down Expand Up @@ -383,6 +419,26 @@ void afterEachInnerMethod() {
}
}

@ExtendWith(FooMethodLevelCallbacks.class)
private static class DynamicTestTestCase {

@BeforeEach
void beforeEach() {
callSequence.add("beforeEachMethod");
}

@TestFactory
List<DynamicTest> localTest() {
return Arrays.asList(dynamicTest("dynamicTest1", () -> callSequence.add("localTestMethod1")),
dynamicTest("dynamicTest2", () -> callSequence.add("localTestMethod2")));
}

@AfterEach
void afterEach() {
callSequence.add("afterEachMethod");
}
}

@ExtendWith({ FooMethodLevelCallbacks.class, ExceptionThrowingBeforeEachCallback.class,
BarMethodLevelCallbacks.class })
private static class ExceptionInBeforeEachCallbackTestCase {
Expand Down