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

Better support for junit5 parameterised tests #687

Merged
merged 2 commits into from
Nov 30, 2021
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 @@ -22,6 +22,7 @@
import io.qameta.allure.SeverityLevel;
import io.qameta.allure.model.FixtureResult;
import io.qameta.allure.model.Label;
import io.qameta.allure.model.Parameter;
import io.qameta.allure.model.Stage;
import io.qameta.allure.model.Status;
import io.qameta.allure.model.StatusDetails;
Expand Down Expand Up @@ -89,12 +90,18 @@
})
public class AllureJunitPlatform implements TestExecutionListener {

public static final String ALLURE_PARAMETER = "allure.parameter";
public static final String ALLURE_PARAMETER_VALUE_KEY = "value";
public static final String ALLURE_PARAMETER_MODE_KEY = "mode";
public static final String ALLURE_PARAMETER_EXCLUDED_KEY = "excluded";

public static final String ALLURE_FIXTURE = "allure.fixture";
public static final String PREPARE = "prepare";
public static final String TEAR_DOWN = "tear_down";
public static final String EVENT_START = "start";
public static final String EVENT_STOP = "stop";
public static final String EVENT_FAILURE = "failure";

public static final String JUNIT_PLATFORM_UNIQUE_ID = "junit.platform.uniqueid";
private static final Logger LOGGER = LoggerFactory.getLogger(AllureJunitPlatform.class);
private static final String STDOUT = "stdout";
Expand Down Expand Up @@ -188,40 +195,17 @@ public void executionSkipped(final TestIdentifier testIdentifier,
);
}

@SuppressWarnings({"ReturnCount", "PMD.NcssCount"})
@SuppressWarnings({"ReturnCount", "PMD.NcssCount", "CyclomaticComplexity"})
@Override
public void reportingEntryPublished(final TestIdentifier testIdentifier,
final ReportEntry entry) {
final Map<String, String> keyValuePairs = entry.getKeyValuePairs();
if (keyValuePairs.containsKey(ALLURE_FIXTURE)) {
final String type = keyValuePairs.get(ALLURE_FIXTURE);
final String event = keyValuePairs.get("event");

// skip for invalid events
if (Objects.isNull(type) || Objects.isNull(event)) {
return;
}

switch (event) {
case EVENT_START:
final Optional<String> maybeParent = containers.get(testIdentifier);
if (!maybeParent.isPresent()) {
return;
}
final String parentUuid = maybeParent.get();
startFixture(parentUuid, type, keyValuePairs);
return;
case EVENT_FAILURE:
failFixture(keyValuePairs);
resetContext(testIdentifier);
return;
case EVENT_STOP:
stopFixture(keyValuePairs);
resetContext(testIdentifier);
return;
default:
break;
}
processFixtureEvent(testIdentifier, keyValuePairs);
return;
}
if (keyValuePairs.containsKey(ALLURE_PARAMETER)) {
processParameterEvent(keyValuePairs);
return;
}

Expand All @@ -236,6 +220,63 @@ public void reportingEntryPublished(final TestIdentifier testIdentifier,

}

private void processParameterEvent(final Map<String, String> keyValuePairs) {
final String name = keyValuePairs.get(ALLURE_PARAMETER);
final String value = keyValuePairs.get(ALLURE_PARAMETER_VALUE_KEY);

final Parameter parameter = ResultsUtils.createParameter(name, value);
if (keyValuePairs.containsKey(ALLURE_PARAMETER_MODE_KEY)) {
final String modeString = keyValuePairs.get(ALLURE_PARAMETER_MODE_KEY);
Stream.of(Parameter.Mode.values())
.filter(mode -> mode.name().equalsIgnoreCase(modeString))
.findAny()
.ifPresent(parameter::setMode);
}
if (keyValuePairs.containsKey(ALLURE_PARAMETER_EXCLUDED_KEY)) {
final String excludedString = keyValuePairs.get(ALLURE_PARAMETER_EXCLUDED_KEY);
Optional.ofNullable(excludedString)
.map(Boolean::parseBoolean)
.ifPresent(parameter::setExcluded);
}

getLifecycle().updateTestCase(tr -> tr.getParameters()
.add(parameter)
);
}

@SuppressWarnings({"ReturnCount"})
private void processFixtureEvent(final TestIdentifier testIdentifier,
final Map<String, String> keyValuePairs) {
final String type = keyValuePairs.get(ALLURE_FIXTURE);
final String event = keyValuePairs.get("event");

// skip for invalid events
if (Objects.isNull(type) || Objects.isNull(event)) {
return;
}

switch (event) {
case EVENT_START:
final Optional<String> maybeParent = containers.get(testIdentifier);
if (!maybeParent.isPresent()) {
return;
}
final String parentUuid = maybeParent.get();
startFixture(parentUuid, type, keyValuePairs);
return;
case EVENT_FAILURE:
failFixture(keyValuePairs);
resetContext(testIdentifier);
return;
case EVENT_STOP:
stopFixture(keyValuePairs);
resetContext(testIdentifier);
return;
default:
break;
}
}

private void resetContext(final TestIdentifier testIdentifier) {
// in case of fixtures that reported within a test we need to return current
// test case uuid to allure thread local storage
Expand Down Expand Up @@ -362,13 +403,38 @@ private void startTestCase(final TestIdentifier testIdentifier) {
final Optional<Class<?>> testClass = testSource
.flatMap(AllureJunitPlatformUtils::getTestClass);

final boolean testTemplate = "test-template-invocation"
.equals(testIdentifier.getUniqueIdObject().getLastSegment().getType());

final Optional<TestIdentifier> maybeParent = Optional.of(testPlanStorage)
.map(ThreadLocal::get)
.flatMap(tp -> tp.getParent(testIdentifier));

final TestResult result = new TestResult()
.setUuid(uuid)
.setName(testIdentifier.getDisplayName())
.setName(testTemplate && maybeParent.isPresent()
? maybeParent.get().getDisplayName() + " " + testIdentifier.getDisplayName()
: testIdentifier.getDisplayName()
)
.setLabels(getTags(testIdentifier))
.setTestCaseId(testTemplate
? maybeParent.map(TestIdentifier::getUniqueId)
.orElseGet(testIdentifier::getUniqueId)
: testIdentifier.getUniqueId()
)
.setHistoryId(getHistoryId(testIdentifier))
.setStage(Stage.RUNNING);

if (testTemplate) {
// history id is ignored in Allure TestOps, so we add a hidden parameter
// to make sure different results are not considered as retries
result.getParameters().add(new Parameter()
.setMode(Parameter.Mode.HIDDEN)
.setName("UniqueId")
.setValue(testIdentifier.getUniqueId())
);
}

result.getLabels().addAll(getProvidedLabels());

result.getLabels().add(getJUnitPlatformUniqueId(testIdentifier));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
import io.qameta.allure.junitplatform.features.OwnerTest;
import io.qameta.allure.junitplatform.features.ParallelTests;
import io.qameta.allure.junitplatform.features.ParameterisedTests;
import io.qameta.allure.junitplatform.features.ParameterisedTestsWithDisplayName;
import io.qameta.allure.junitplatform.features.PassedTests;
import io.qameta.allure.junitplatform.features.RepeatedTests;
import io.qameta.allure.junitplatform.features.ReportEntryParameter;
import io.qameta.allure.junitplatform.features.SeverityTest;
import io.qameta.allure.junitplatform.features.SkippedInBeforeAllTests;
import io.qameta.allure.junitplatform.features.SkippedTests;
Expand All @@ -53,6 +55,7 @@
import io.qameta.allure.model.Attachment;
import io.qameta.allure.model.Label;
import io.qameta.allure.model.Link;
import io.qameta.allure.model.Parameter;
import io.qameta.allure.model.Stage;
import io.qameta.allure.model.Status;
import io.qameta.allure.model.StatusDetails;
Expand Down Expand Up @@ -209,9 +212,9 @@ void shouldProcessBrokenInAfterAllTests() {
tr -> Optional.of(tr).map(TestResult::getStatusDetails).map(StatusDetails::getMessage).orElse(null))
.containsExactlyInAnyOrder(
tuple("BrokenInAfterAllTests", Status.BROKEN, "Exception in @AfterAll"),
tuple("[1] value=a", Status.PASSED, null),
tuple("[2] value=b", Status.PASSED, null),
tuple("[3] value=c", Status.PASSED, null),
tuple("parameterisedTest(String) [1] value=a", Status.PASSED, null),
tuple("parameterisedTest(String) [2] value=b", Status.PASSED, null),
tuple("parameterisedTest(String) [3] value=c", Status.PASSED, null),
tuple("test1()", Status.PASSED, null),
tuple("test2()", Status.PASSED, null)
);
Expand Down Expand Up @@ -323,7 +326,10 @@ void shouldProcessParametrisedTests() {
.hasSize(2)
.filteredOn(hasStatus(Status.PASSED))
.flatExtracting(TestResult::getName)
.containsExactlyInAnyOrder("[1] argument=Hello", "[2] argument=World");
.containsExactlyInAnyOrder(
"testWithStringParameter(String) [1] argument=Hello",
"testWithStringParameter(String) [2] argument=World"
);
}

@Test
Expand Down Expand Up @@ -773,4 +779,51 @@ void shouldPopulateTestCaseId() {
.first()
.isEqualTo("[engine:junit-jupiter]/[class:io.qameta.allure.junitplatform.features.JupiterUniqueIdTest]/[method:jupiterTest()]");
}

@AllureFeatures.Parameters
@Test
void shouldAllowUsageOfDisplayForParameterisedTests() {
final AllureResults results = runClasses(ParameterisedTestsWithDisplayName.class);
final List<TestResult> testResults = results.getTestResults();
assertThat(testResults)
.extracting(
TestResult::getName
)
.containsExactlyInAnyOrder(
"Second Test [1] value=a",
"Second Test [2] value=b"
);
}

@AllureFeatures.Parameters
@Test
void shouldProcessAllureParameterReportingEvents() {
final AllureResults results = runClasses(ReportEntryParameter.class);
final List<TestResult> testResults = results.getTestResults();
assertThat(testResults)
.filteredOn("name", "simpleParameterEvent(TestReporter)")
.flatExtracting(TestResult::getParameters)
.extracting(Parameter::getName, Parameter::getValue, Parameter::getMode, Parameter::getExcluded)
.containsExactlyInAnyOrder(
tuple("some parameter name", "some parameter value", null, null)
);
assertThat(testResults)
.filteredOn("name", "multipleParameterEvent(TestReporter)")
.flatExtracting(TestResult::getParameters)
.extracting(Parameter::getName, Parameter::getValue, Parameter::getMode, Parameter::getExcluded)
.containsExactlyInAnyOrder(
tuple("first name", "first value", null, null),
tuple("second name", "second value", null, null),
tuple("third name", "third value", null, null)
);
assertThat(testResults)
.filteredOn("name", "modeAndExcluded(TestReporter)")
.flatExtracting(TestResult::getParameters)
.extracting(Parameter::getName, Parameter::getValue, Parameter::getMode, Parameter::getExcluded)
.containsExactlyInAnyOrder(
tuple("hidden excluded", "hidden excluded value", Parameter.Mode.HIDDEN, true),
tuple("default excluded", "default excluded value", Parameter.Mode.DEFAULT, true),
tuple("masked not excluded", "masked not excluded value", Parameter.Mode.MASKED, false)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019 Qameta Software OÜ
*
* 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 io.qameta.allure.junitplatform.features;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

/**
* @author charlie (Dmitry Baev).
*/
public class ParameterisedTestsWithDisplayName {

@DisplayName("Second Test")
@ParameterizedTest
@ValueSource(strings = {"a", "b"})
void first(String value) {
}

}
Loading