Skip to content
Merged
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 @@ -115,8 +115,18 @@ public class AllureJunitPlatform implements TestExecutionListener {

private final ThreadLocal<TestPlan> testPlanStorage = new InheritableThreadLocal<>();

private final Uuids tests = new Uuids();
private final Uuids containers = new Uuids();
private final ThreadLocal<Uuids> tests = new InheritableThreadLocal<Uuids>() {
@Override
protected Uuids initialValue() {
return new Uuids();
}
};
private final ThreadLocal<Uuids> containers = new InheritableThreadLocal<Uuids>() {
@Override
protected Uuids initialValue() {
return new Uuids();
}
};

private final AllureLifecycle lifecycle;

Expand All @@ -135,11 +145,15 @@ public AllureLifecycle getLifecycle() {
@Override
public void testPlanExecutionStarted(final TestPlan testPlan) {
testPlanStorage.set(testPlan);
tests.set(new Uuids());
containers.set(new Uuids());
}

@Override
public void testPlanExecutionFinished(final TestPlan testPlan) {
testPlanStorage.remove();
tests.remove();
containers.remove();
}

@Override
Expand Down Expand Up @@ -199,7 +213,7 @@ public void executionSkipped(final TestIdentifier testIdentifier,
);
}

@SuppressWarnings({"ReturnCount", "PMD.NcssCount", "CyclomaticComplexity" })
@SuppressWarnings({"ReturnCount", "PMD.NcssCount", "CyclomaticComplexity"})
@Override
public void reportingEntryPublished(final TestIdentifier testIdentifier,
final ReportEntry entry) {
Expand Down Expand Up @@ -270,7 +284,7 @@ private void processParameterEvent(final Map<String, String> keyValuePairs) {
);
}

@SuppressWarnings({"ReturnCount" })
@SuppressWarnings({"ReturnCount"})
private void processFixtureEvent(final TestIdentifier testIdentifier,
final Map<String, String> keyValuePairs) {
final String type = keyValuePairs.get(ALLURE_FIXTURE);
Expand All @@ -283,7 +297,7 @@ private void processFixtureEvent(final TestIdentifier testIdentifier,

switch (event) {
case EVENT_START:
final Optional<String> maybeParent = containers.get(testIdentifier);
final Optional<String> maybeParent = getContainer(testIdentifier);
if (!maybeParent.isPresent()) {
return;
}
Expand All @@ -308,7 +322,7 @@ private void resetContext(final TestIdentifier testIdentifier) {
// test case uuid to allure thread local storage
Optional.of(testIdentifier)
.filter(TestIdentifier::isTest)
.flatMap(tests::get)
.flatMap((TestIdentifier t) -> getTest(t))
.ifPresent(Allure.getLifecycle()::setCurrentTestCase);
}

Expand All @@ -333,7 +347,7 @@ protected Status getStatus(final Throwable throwable) {
}

private void startTestContainer(final TestIdentifier testIdentifier) {
final String uuid = containers.getOrCreate(testIdentifier);
final String uuid = getOrCreateContainer(testIdentifier);
final TestResultContainer result = new TestResultContainer()
.setUuid(uuid)
.setName(testIdentifier.getDisplayName());
Expand All @@ -342,7 +356,7 @@ private void startTestContainer(final TestIdentifier testIdentifier) {
}

private void stopTestContainer(final TestIdentifier testIdentifier) {
final Optional<String> maybeUuid = containers.get(testIdentifier);
final Optional<String> maybeUuid = getContainer(testIdentifier);
if (!maybeUuid.isPresent()) {
return;
}
Expand All @@ -353,14 +367,14 @@ private void stopTestContainer(final TestIdentifier testIdentifier) {
.orElseGet(Collections::emptySet)
.stream()
.filter(TestIdentifier::isTest)
.map(tests::get)
.map(this::getTest)
.filter(Optional::isPresent)
.map(Optional::get)
.distinct()
.collect(Collectors.toCollection(ArrayList::new));

if (testIdentifier.isTest()) {
tests.get(testIdentifier).ifPresent(children::add);
getTest(testIdentifier).ifPresent(children::add);
}

getLifecycle().updateTestContainer(uuid, container -> container.setChildren(children));
Expand Down Expand Up @@ -421,7 +435,7 @@ private void stopFixture(final Map<String, String> keyValue) {

@SuppressWarnings("PMD.NcssCount")
private void startTestCase(final TestIdentifier testIdentifier) {
final String uuid = tests.getOrCreate(testIdentifier);
final String uuid = getOrCreateTest(testIdentifier);

final Optional<TestSource> testSource = testIdentifier.getSource();
final Optional<Method> testMethod = testSource
Expand Down Expand Up @@ -519,7 +533,7 @@ private void startTestCase(final TestIdentifier testIdentifier) {
private void stopTestCase(final TestIdentifier testIdentifier,
final Status status,
final StatusDetails statusDetails) {
final Optional<String> maybeUuid = tests.get(testIdentifier);
final Optional<String> maybeUuid = getTest(testIdentifier);
if (!maybeUuid.isPresent()) {
return;
}
Expand Down Expand Up @@ -614,6 +628,22 @@ private Label getJUnitPlatformUniqueId(final TestIdentifier testIdentifier) {
return label;
}

private Optional<String> getContainer(final TestIdentifier testIdentifier) {
return containers.get().get(testIdentifier);
}

private String getOrCreateContainer(final TestIdentifier testIdentifier) {
return containers.get().getOrCreate(testIdentifier);
}

private Optional<String> getTest(final TestIdentifier testIdentifier) {
return tests.get().get(testIdentifier);
}

private String getOrCreateTest(final TestIdentifier testIdentifier) {
return tests.get().getOrCreate(testIdentifier);
}

private static class Uuids {

private final Map<TestIdentifier, String> storage = new ConcurrentHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
package io.qameta.allure.junitplatform;

import io.github.glytching.junit.extension.system.SystemProperty;
import io.qameta.allure.Allure;
import io.qameta.allure.AllureLifecycle;
import io.qameta.allure.Issue;
import io.qameta.allure.aspects.AttachmentsAspects;
import io.qameta.allure.aspects.StepsAspects;
import io.qameta.allure.junitplatform.features.AllureIdAnnotationSupport;
import io.qameta.allure.junitplatform.features.BrokenInAfterAllTests;
import io.qameta.allure.junitplatform.features.BrokenInBeforeAllTests;
Expand Down Expand Up @@ -63,9 +67,16 @@
import io.qameta.allure.model.TestResult;
import io.qameta.allure.test.AllureFeatures;
import io.qameta.allure.test.AllureResults;
import io.qameta.allure.test.AllureResultsWriterStub;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.ResourceLock;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.core.LauncherConfig;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;

import java.nio.charset.StandardCharsets;
import java.util.Collection;
Expand Down Expand Up @@ -826,4 +837,55 @@ void shouldProcessAllureParameterReportingEvents() {
tuple("masked not excluded", "masked not excluded value", Parameter.Mode.MASKED, false)
);
}

@Test
@AllureFeatures.Retries
void shouldSetDifferentUuidsInDifferentRuns() {
final AllureResultsWriterStub results = new AllureResultsWriterStub();
final AllureLifecycle lifecycle = new AllureLifecycle(results);

final LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.filters(new AllurePostDiscoveryFilter(null))
.selectors(DiscoverySelectors.selectClass(OneTest.class))
.build();

final LauncherConfig config = LauncherConfig.builder()
.enableTestExecutionListenerAutoRegistration(false)
.addTestExecutionListeners(new AllureJunitPlatform(lifecycle))
.enablePostDiscoveryFilterAutoRegistration(false)
.build();
final Launcher launcher = LauncherFactory.create(config);

final AllureLifecycle defaultLifecycle = Allure.getLifecycle();
try {
Allure.setLifecycle(lifecycle);
StepsAspects.setLifecycle(lifecycle);
AttachmentsAspects.setLifecycle(lifecycle);

// execute request twice
launcher.execute(request);
launcher.execute(request);
} finally {
Allure.setLifecycle(defaultLifecycle);
StepsAspects.setLifecycle(defaultLifecycle);
AttachmentsAspects.setLifecycle(defaultLifecycle);
}


final List<TestResult> testResults = results.getTestResults();
assertThat(testResults)
.hasSize(2);

final TestResult tr1 = testResults.get(0);
final TestResult tr2 = testResults.get(1);

assertThat(tr1)
.extracting(TestResult::getUuid)
.isNotEqualTo(tr2.getUuid());

assertThat(tr1)
.extracting(TestResult::getHistoryId)
.isEqualTo(tr2.getHistoryId());

}
}