Skip to content

Commit

Permalink
Implement parallel test
Browse files Browse the repository at this point in the history
  • Loading branch information
tddang-linagora committed Jul 18, 2024
1 parent 019eb5d commit 2125368
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.tmail.base;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Optional;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.engine.execution.BeforeEachMethodAdapter;
import org.junit.jupiter.engine.extension.ExtensionRegistry;

public class CustomParameterResolver implements BeforeEachMethodAdapter, ParameterResolver {

private ParameterResolver parameterisedTestParameterResolver = null;

@Override
public void invokeBeforeEachMethod(ExtensionContext context, ExtensionRegistry registry)
throws Throwable {
Optional<ParameterResolver> resolverOptional = registry.getExtensions(ParameterResolver.class)
.stream()
.filter(parameterResolver -> parameterResolver.getClass().getName()
.contains("ParameterizedTestParameterResolver"))
.findFirst();
if (!resolverOptional.isPresent()) {
throw new IllegalStateException(
"ParameterizedTestParameterResolver missed in the registry. Probably it's not a Parameterized Test");
} else {
parameterisedTestParameterResolver = resolverOptional.get();
}
}

@Override
public boolean supportsParameter(ParameterContext parameterContext,
ExtensionContext extensionContext) throws ParameterResolutionException {
if (isExecutedOnAfterOrBeforeMethod(parameterContext)) {
ParameterContext pContext = getMappedContext(parameterContext, extensionContext);
return parameterisedTestParameterResolver.supportsParameter(pContext, extensionContext);
}
return false;
}

@Override
public Object resolveParameter(ParameterContext parameterContext,
ExtensionContext extensionContext) throws ParameterResolutionException {
return parameterisedTestParameterResolver.resolveParameter(
getMappedContext(parameterContext, extensionContext), extensionContext);
}

private MappedParameterContext getMappedContext(ParameterContext parameterContext,
ExtensionContext extensionContext) {
return new MappedParameterContext(
parameterContext.getIndex(),
extensionContext.getRequiredTestMethod().getParameters()[parameterContext.getIndex()],
Optional.of(parameterContext.getTarget()));
}

private boolean isExecutedOnAfterOrBeforeMethod(ParameterContext parameterContext) {
return Arrays.stream(parameterContext.getDeclaringExecutable().getDeclaredAnnotations())
.anyMatch(this::isAfterEachOrBeforeEachAnnotation);
}

private boolean isAfterEachOrBeforeEachAnnotation(Annotation annotation) {
return annotation.annotationType() == BeforeEach.class
|| annotation.annotationType() == AfterEach.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.tmail.base;

import java.lang.annotation.Annotation;
import java.lang.reflect.Parameter;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.platform.commons.util.AnnotationUtils;

public class MappedParameterContext implements ParameterContext {

private final int index;
private final Parameter parameter;
private final Optional<Object> target;

public MappedParameterContext(int index, Parameter parameter,
Optional<Object> target) {
this.index = index;
this.parameter = parameter;
this.target = target;
}

@Override
public boolean isAnnotated(Class<? extends Annotation> annotationType) {
return AnnotationUtils.isAnnotated(parameter, annotationType);
}

@Override
public <A extends Annotation> Optional<A> findAnnotation(Class<A> annotationType) {
return Optional.empty();
}

@Override
public <A extends Annotation> List<A> findRepeatableAnnotations(Class<A> annotationType) {
return null;
}

@Override
public int getIndex() {
return index;
}

@Override
public Parameter getParameter() {
return parameter;
}

@Override
public Optional<Object> getTarget() {
return target;
}
}
37 changes: 23 additions & 14 deletions tmail_selenium/src/main/java/com/tmail/base/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

Expand All @@ -16,16 +19,18 @@
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

@ExtendWith(CustomParameterResolver.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public abstract class TestBase {
private static Playwright playwright;
private static Browser browser;
private static BrowserContext browserContext;
private static Page page;
private Playwright playwright;
private Browser browser;
private BrowserContext browserContext;
private Page page;

protected BaseScenario scenario;
protected Properties properties;

private Boolean runHeadlessTest = false;
private static Boolean runHeadlessTest = true;

public TestBase() {
properties = new Properties();
Expand All @@ -38,10 +43,21 @@ public TestBase() {
}

@BeforeAll
static void setUpAll() {
void setUpAll() {
playwright = Playwright.create();
}

@BeforeEach
public void setUp(SupportedPlatform supportedPlatform) {
browser = switch (supportedPlatform) {
case CHROME -> playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(runHeadlessTest));
case FIREFOX -> playwright.firefox().launch(new BrowserType.LaunchOptions().setHeadless(runHeadlessTest));
default -> throw new UnsupportedPlatformException();
};
browserContext = browser.newContext();
page = browserContext.newPage();
}

@AfterEach
public void tearDown() {
page.close();
Expand All @@ -50,7 +66,7 @@ public void tearDown() {
}

@AfterAll
static void tearDownAll() {
void tearDownAll() {
playwright.close();
}

Expand All @@ -61,13 +77,6 @@ static Stream<SupportedPlatform> supportedPlatforms() {
@ParameterizedTest
@MethodSource("supportedPlatforms")
public void testScenario(SupportedPlatform supportedPlatform) {
browser = switch (supportedPlatform) {
case CHROME -> playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(runHeadlessTest));
case FIREFOX -> playwright.firefox().launch(new BrowserType.LaunchOptions().setHeadless(runHeadlessTest));
default -> throw new UnsupportedPlatformException();
};
browserContext = browser.newContext();
page = browserContext.newPage();
scenario.execute(page);
}
}
5 changes: 5 additions & 0 deletions tmail_selenium/src/test/resources/junit-platform.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = same_thread
junit.jupiter.execution.parallel.mode.classes.default = concurrent
junit.jupiter.execution.parallel.config.strategy=dynamic
junit.jupiter.execution.parallel.config.dynamic.factor=0.5

0 comments on commit 2125368

Please sign in to comment.