From b412136148ffcb4a2148e16994a923ae989a3b32 Mon Sep 17 00:00:00 2001 From: Dmitry Baev Date: Mon, 29 Nov 2021 18:55:59 +0300 Subject: [PATCH 1/2] better support for junit5 parameterised tests --- .../junitplatform/AllureJunitPlatform.java | 126 +++++++++++++----- .../AllureJunitPlatformTest.java | 61 ++++++++- .../ParameterisedTestsWithDisplayName.java | 33 +++++ .../features/ReportEntryParameter.java | 93 +++++++++++++ .../io/qameta/allure/junit5/AllureJunit5.java | 49 +++++++ .../allure/junit5/AllureJunit5Test.java | 85 ++++++++++++ .../junit5/features/ParameterisedTests.java | 49 +++++++ .../junit5/features/SkipOtherInjectables.java | 37 +++++ 8 files changed, 499 insertions(+), 34 deletions(-) create mode 100644 allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/ParameterisedTestsWithDisplayName.java create mode 100644 allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/ReportEntryParameter.java create mode 100644 allure-junit5/src/test/java/io/qameta/allure/junit5/features/ParameterisedTests.java create mode 100644 allure-junit5/src/test/java/io/qameta/allure/junit5/features/SkipOtherInjectables.java diff --git a/allure-junit-platform/src/main/java/io/qameta/allure/junitplatform/AllureJunitPlatform.java b/allure-junit-platform/src/main/java/io/qameta/allure/junitplatform/AllureJunitPlatform.java index a4164acdd..2b10b4eb2 100644 --- a/allure-junit-platform/src/main/java/io/qameta/allure/junitplatform/AllureJunitPlatform.java +++ b/allure-junit-platform/src/main/java/io/qameta/allure/junitplatform/AllureJunitPlatform.java @@ -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; @@ -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"; @@ -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 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 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; } @@ -236,6 +220,62 @@ public void reportingEntryPublished(final TestIdentifier testIdentifier, } + private void processParameterEvent(final Map 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) + ); + } + + private void processFixtureEvent(final TestIdentifier testIdentifier, + final Map 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 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 @@ -362,13 +402,39 @@ private void startTestCase(final TestIdentifier testIdentifier) { final Optional> testClass = testSource .flatMap(AllureJunitPlatformUtils::getTestClass); + final boolean testTemplate = "test-template-invocation" + .equals(testIdentifier.getUniqueIdObject().getLastSegment().getType()); + + final Optional maybeParent = Optional.of(testPlanStorage) + .map(ThreadLocal::get) + .flatMap(tp -> tp.getParent(testIdentifier)); + + final TestIdentifier parent = maybeParent.orElse(null); + final TestResult result = new TestResult() .setUuid(uuid) - .setName(testIdentifier.getDisplayName()) + .setName(testTemplate + ? parent.getDisplayName() + " " + testIdentifier.getDisplayName() + : testIdentifier.getDisplayName() + ) .setLabels(getTags(testIdentifier)) + .setTestCaseId(testTemplate + ? maybeParent.map(TestIdentifier::getUniqueId).orElse(null) + : 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)); diff --git a/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/AllureJunitPlatformTest.java b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/AllureJunitPlatformTest.java index 3ee5ed814..4c8ee1121 100644 --- a/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/AllureJunitPlatformTest.java +++ b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/AllureJunitPlatformTest.java @@ -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; @@ -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; @@ -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) ); @@ -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 @@ -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 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 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) + ); + } } diff --git a/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/ParameterisedTestsWithDisplayName.java b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/ParameterisedTestsWithDisplayName.java new file mode 100644 index 000000000..dfb83eff8 --- /dev/null +++ b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/ParameterisedTestsWithDisplayName.java @@ -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) { + } + +} diff --git a/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/ReportEntryParameter.java b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/ReportEntryParameter.java new file mode 100644 index 000000000..c7dc820d3 --- /dev/null +++ b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/ReportEntryParameter.java @@ -0,0 +1,93 @@ +/* + * 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 io.qameta.allure.model.Parameter; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestReporter; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +import static io.qameta.allure.junitplatform.AllureJunitPlatform.ALLURE_PARAMETER; +import static io.qameta.allure.junitplatform.AllureJunitPlatform.ALLURE_PARAMETER_EXCLUDED_KEY; +import static io.qameta.allure.junitplatform.AllureJunitPlatform.ALLURE_PARAMETER_MODE_KEY; +import static io.qameta.allure.junitplatform.AllureJunitPlatform.ALLURE_PARAMETER_VALUE_KEY; + +/** + * @author charlie (Dmitry Baev). + */ +public class ReportEntryParameter { + + @Test + void simpleParameterEvent(TestReporter testReporter) { + testReporter.publishEntry(buildEvent("some parameter name", "some parameter value")); + } + + @Test + void multipleParameterEvent(TestReporter testReporter) { + testReporter.publishEntry(buildEvent("first name", "first value")); + testReporter.publishEntry(buildEvent("second name", "second value")); + testReporter.publishEntry(buildEvent("third name", "third value")); + } + + @Test + void modeAndExcluded(TestReporter testReporter) { + testReporter.publishEntry(buildEvent( + "hidden excluded", + "hidden excluded value", + Parameter.Mode.HIDDEN, + true + )); + testReporter.publishEntry(buildEvent( + "default excluded", + "default excluded value", + Parameter.Mode.DEFAULT, + true + )); + testReporter.publishEntry(buildEvent( + "masked not excluded", + "masked not excluded value", + Parameter.Mode.MASKED, + false + )); + } + + private Map buildEvent(final String name, + final String value) { + return buildEvent(name, value, null, null); + } + + private Map buildEvent(final String name, + final String value, + final Parameter.Mode mode, + final Boolean excluded) { + final Map data = new HashMap<>(); + data.put(ALLURE_PARAMETER, name); + data.put(ALLURE_PARAMETER_VALUE_KEY, value); + if (Objects.nonNull(mode)) { + data.put(ALLURE_PARAMETER_MODE_KEY, mode.name()); + } + if (Objects.nonNull(excluded)) { + data.put(ALLURE_PARAMETER_EXCLUDED_KEY, excluded.toString()); + } + + return data; + } + + +} diff --git a/allure-junit5/src/main/java/io/qameta/allure/junit5/AllureJunit5.java b/allure-junit5/src/main/java/io/qameta/allure/junit5/AllureJunit5.java index 9f2e2b04f..2ac552bd8 100644 --- a/allure-junit5/src/main/java/io/qameta/allure/junit5/AllureJunit5.java +++ b/allure-junit5/src/main/java/io/qameta/allure/junit5/AllureJunit5.java @@ -15,20 +15,28 @@ */ package io.qameta.allure.junit5; +import io.qameta.allure.Param; import io.qameta.allure.model.Status; import io.qameta.allure.model.StatusDetails; +import io.qameta.allure.util.ObjectUtils; import io.qameta.allure.util.ResultsUtils; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.InvocationInterceptor; import org.junit.jupiter.api.extension.ReflectiveInvocationContext; import java.lang.reflect.Method; +import java.lang.reflect.Parameter; import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.UUID; +import java.util.stream.Stream; import static io.qameta.allure.junitplatform.AllureJunitPlatform.ALLURE_FIXTURE; +import static io.qameta.allure.junitplatform.AllureJunitPlatform.ALLURE_PARAMETER; +import static io.qameta.allure.junitplatform.AllureJunitPlatform.ALLURE_PARAMETER_EXCLUDED_KEY; +import static io.qameta.allure.junitplatform.AllureJunitPlatform.ALLURE_PARAMETER_MODE_KEY; +import static io.qameta.allure.junitplatform.AllureJunitPlatform.ALLURE_PARAMETER_VALUE_KEY; import static io.qameta.allure.junitplatform.AllureJunitPlatform.EVENT_FAILURE; import static io.qameta.allure.junitplatform.AllureJunitPlatform.EVENT_START; import static io.qameta.allure.junitplatform.AllureJunitPlatform.EVENT_STOP; @@ -41,6 +49,47 @@ @SuppressWarnings("MultipleStringLiterals") public class AllureJunit5 implements InvocationInterceptor { + @Override + public void interceptTestTemplateMethod(final Invocation invocation, + final ReflectiveInvocationContext invocationContext, + final ExtensionContext extensionContext) throws Throwable { + sendParameterEvent(invocationContext, extensionContext); + invocation.proceed(); + } + + private void sendParameterEvent(final ReflectiveInvocationContext invocationContext, + final ExtensionContext extensionContext) { + final Parameter[] parameters = invocationContext.getExecutable().getParameters(); + for (int i = 0; i < parameters.length; i++) { + final Parameter parameter = parameters[i]; + + final Class parameterType = parameter.getType(); + // Skip default jupiter injectables as TestInfo, TestReporter and TempDirectory + if (parameterType.getPackage().getName().startsWith("org.junit.jupiter.api")) { + continue; + } + final Object value = invocationContext.getArguments().get(i); + final Map map = new HashMap<>(); + map.put(ALLURE_PARAMETER, parameter.getName()); + map.put(ALLURE_PARAMETER_VALUE_KEY, ObjectUtils.toString(value)); + + Stream.of(parameter.getAnnotationsByType(Param.class)) + .findFirst() + .ifPresent(param -> { + Stream.of(param.value(), param.name()) + .map(String::trim) + .filter(name -> name.length() > 0) + .findFirst() + .ifPresent(name -> map.put(ALLURE_PARAMETER, name)); + + map.put(ALLURE_PARAMETER_MODE_KEY, param.mode().name()); + map.put(ALLURE_PARAMETER_EXCLUDED_KEY, Boolean.toString(param.excluded())); + }); + + extensionContext.publishReportEntry(map); + } + } + @Override public void interceptBeforeAllMethod( final Invocation invocation, diff --git a/allure-junit5/src/test/java/io/qameta/allure/junit5/AllureJunit5Test.java b/allure-junit5/src/test/java/io/qameta/allure/junit5/AllureJunit5Test.java index db61285fe..41590e3cd 100644 --- a/allure-junit5/src/test/java/io/qameta/allure/junit5/AllureJunit5Test.java +++ b/allure-junit5/src/test/java/io/qameta/allure/junit5/AllureJunit5Test.java @@ -24,8 +24,11 @@ import io.qameta.allure.junit5.features.AllFixtureSupport; import io.qameta.allure.junit5.features.BeforeEachFixtureFailureSupport; import io.qameta.allure.junit5.features.EachFixtureSupport; +import io.qameta.allure.junit5.features.ParameterisedTests; +import io.qameta.allure.junit5.features.SkipOtherInjectables; import io.qameta.allure.junitplatform.AllureJunitPlatform; import io.qameta.allure.model.FixtureResult; +import io.qameta.allure.model.Parameter; import io.qameta.allure.model.Status; import io.qameta.allure.model.StepResult; import io.qameta.allure.model.TestResult; @@ -54,6 +57,88 @@ @SuppressWarnings("unchecked") class AllureJunit5Test { + @Test + void shouldSupportParametersForParameterisedTests() { + final AllureResults results = runClasses(ParameterisedTests.class); + + assertThat(results.getTestResults()) + .filteredOn("fullName", "io.qameta.allure.junit5.features.ParameterisedTests.first") + .filteredOn("name", "[1] value=a") + .flatExtracting(TestResult::getParameters) + .extracting(Parameter::getName, Parameter::getValue, Parameter::getMode, Parameter::getExcluded) + .containsExactlyInAnyOrder( + tuple("value", "a", null, null) + ); + + assertThat(results.getTestResults()) + .filteredOn("fullName", "io.qameta.allure.junit5.features.ParameterisedTests.first") + .filteredOn("name", "[2] value=b") + .flatExtracting(TestResult::getParameters) + .extracting(Parameter::getName, Parameter::getValue, Parameter::getMode, Parameter::getExcluded) + .containsExactlyInAnyOrder( + tuple("value", "b", null, null) + ); + + } + + @Test + void shouldSupportParamAnnotationForParameters() { + final AllureResults results = runClasses(ParameterisedTests.class); + + assertThat(results.getTestResults()) + .filteredOn("fullName", "io.qameta.allure.junit5.features.ParameterisedTests.third") + .filteredOn("name", "[1] value=a") + .flatExtracting(TestResult::getParameters) + .extracting(Parameter::getName, Parameter::getValue, Parameter::getMode, Parameter::getExcluded) + .containsExactlyInAnyOrder( + tuple("some value", "a", Parameter.Mode.DEFAULT, false) + ); + + assertThat(results.getTestResults()) + .filteredOn("fullName", "io.qameta.allure.junit5.features.ParameterisedTests.third") + .filteredOn("name", "[2] value=b") + .flatExtracting(TestResult::getParameters) + .extracting(Parameter::getName, Parameter::getValue, Parameter::getMode, Parameter::getExcluded) + .containsExactlyInAnyOrder( + tuple("some value", "b", Parameter.Mode.DEFAULT, false) + ); + + } + + @Test + void shouldSkipReportingOfTestInjectablesTestReporterForRegularTest() { + final AllureResults results = runClasses(SkipOtherInjectables.class); + + assertThat(results.getTestResults()) + .filteredOn("fullName", "io.qameta.allure.junit5.features.SkipOtherInjectables.regularTestWithReporterInjection") + .flatExtracting(TestResult::getParameters) + .extracting(Parameter::getName, Parameter::getValue) + .isEmpty(); + } + + @Test + void shouldSkipReportingOfTestInjectablesTestReporterForParameterisedTest() { + final AllureResults results = runClasses(SkipOtherInjectables.class); + + assertThat(results.getTestResults()) + .filteredOn("fullName", "io.qameta.allure.junit5.features.SkipOtherInjectables.testReporterInjection") + .filteredOn("name", "[1] value=a") + .flatExtracting(TestResult::getParameters) + .extracting(Parameter::getName, Parameter::getValue, Parameter::getMode, Parameter::getExcluded) + .containsExactlyInAnyOrder( + tuple("value", "a", null, null) + ); + + assertThat(results.getTestResults()) + .filteredOn("fullName", "io.qameta.allure.junit5.features.SkipOtherInjectables.testReporterInjection") + .filteredOn("name", "[2] value=b") + .flatExtracting(TestResult::getParameters) + .extracting(Parameter::getName, Parameter::getValue, Parameter::getMode, Parameter::getExcluded) + .containsExactlyInAnyOrder( + tuple("value", "b", null, null) + ); + } + @Test void shouldSupportBeforeEachFixture() { final AllureResults results = runClasses(EachFixtureSupport.class); diff --git a/allure-junit5/src/test/java/io/qameta/allure/junit5/features/ParameterisedTests.java b/allure-junit5/src/test/java/io/qameta/allure/junit5/features/ParameterisedTests.java new file mode 100644 index 000000000..e1ab37cf2 --- /dev/null +++ b/allure-junit5/src/test/java/io/qameta/allure/junit5/features/ParameterisedTests.java @@ -0,0 +1,49 @@ +/* + * 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.junit5.features; + +import io.qameta.allure.Param; +import org.junit.jupiter.api.TestReporter; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +/** + * @author charlie (Dmitry Baev). + */ +public class ParameterisedTests { + + @ParameterizedTest + @ValueSource(strings = {"a", "b"}) + void first(String value) { + } + + @ParameterizedTest + @ValueSource(strings = {"a", "b"}) + void second(String value) { + } + + @ParameterizedTest + @ValueSource(strings = {"a", "b"}) + void third(@Param("some value") String value) { + } + + + @ParameterizedTest + @ValueSource(strings = {"a", "b"}) + void testReporterInjection(String value, TestReporter testReporter) { + } + +} diff --git a/allure-junit5/src/test/java/io/qameta/allure/junit5/features/SkipOtherInjectables.java b/allure-junit5/src/test/java/io/qameta/allure/junit5/features/SkipOtherInjectables.java new file mode 100644 index 000000000..4e566370b --- /dev/null +++ b/allure-junit5/src/test/java/io/qameta/allure/junit5/features/SkipOtherInjectables.java @@ -0,0 +1,37 @@ +/* + * 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.junit5.features; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestReporter; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +/** + * @author charlie (Dmitry Baev). + */ +public class SkipOtherInjectables { + + @Test + void regularTestWithReporterInjection(TestReporter testReporter) { + } + + @ParameterizedTest + @ValueSource(strings = {"a", "b"}) + void testReporterInjection(String value, TestReporter testReporter) { + } + +} From 5a16c988d631e3f6ab9a539b5e7ed6e23f0e8298 Mon Sep 17 00:00:00 2001 From: Dmitry Baev Date: Mon, 29 Nov 2021 19:24:12 +0300 Subject: [PATCH 2/2] fix tests --- .../junitplatform/AllureJunitPlatform.java | 10 +++---- .../allure/junit5/AllureJunit5Test.java | 28 ++++++++----------- gradle/quality-configs/pmd/pmd.xml | 4 ++- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/allure-junit-platform/src/main/java/io/qameta/allure/junitplatform/AllureJunitPlatform.java b/allure-junit-platform/src/main/java/io/qameta/allure/junitplatform/AllureJunitPlatform.java index 2b10b4eb2..a81c04e56 100644 --- a/allure-junit-platform/src/main/java/io/qameta/allure/junitplatform/AllureJunitPlatform.java +++ b/allure-junit-platform/src/main/java/io/qameta/allure/junitplatform/AllureJunitPlatform.java @@ -244,6 +244,7 @@ private void processParameterEvent(final Map keyValuePairs) { ); } + @SuppressWarnings({"ReturnCount"}) private void processFixtureEvent(final TestIdentifier testIdentifier, final Map keyValuePairs) { final String type = keyValuePairs.get(ALLURE_FIXTURE); @@ -409,17 +410,16 @@ private void startTestCase(final TestIdentifier testIdentifier) { .map(ThreadLocal::get) .flatMap(tp -> tp.getParent(testIdentifier)); - final TestIdentifier parent = maybeParent.orElse(null); - final TestResult result = new TestResult() .setUuid(uuid) - .setName(testTemplate - ? parent.getDisplayName() + " " + testIdentifier.getDisplayName() + .setName(testTemplate && maybeParent.isPresent() + ? maybeParent.get().getDisplayName() + " " + testIdentifier.getDisplayName() : testIdentifier.getDisplayName() ) .setLabels(getTags(testIdentifier)) .setTestCaseId(testTemplate - ? maybeParent.map(TestIdentifier::getUniqueId).orElse(null) + ? maybeParent.map(TestIdentifier::getUniqueId) + .orElseGet(testIdentifier::getUniqueId) : testIdentifier.getUniqueId() ) .setHistoryId(getHistoryId(testIdentifier)) diff --git a/allure-junit5/src/test/java/io/qameta/allure/junit5/AllureJunit5Test.java b/allure-junit5/src/test/java/io/qameta/allure/junit5/AllureJunit5Test.java index 41590e3cd..c69eeff77 100644 --- a/allure-junit5/src/test/java/io/qameta/allure/junit5/AllureJunit5Test.java +++ b/allure-junit5/src/test/java/io/qameta/allure/junit5/AllureJunit5Test.java @@ -62,20 +62,18 @@ void shouldSupportParametersForParameterisedTests() { final AllureResults results = runClasses(ParameterisedTests.class); assertThat(results.getTestResults()) - .filteredOn("fullName", "io.qameta.allure.junit5.features.ParameterisedTests.first") - .filteredOn("name", "[1] value=a") + .filteredOn("name", "first(String) [1] value=a") .flatExtracting(TestResult::getParameters) .extracting(Parameter::getName, Parameter::getValue, Parameter::getMode, Parameter::getExcluded) - .containsExactlyInAnyOrder( + .contains( tuple("value", "a", null, null) ); assertThat(results.getTestResults()) - .filteredOn("fullName", "io.qameta.allure.junit5.features.ParameterisedTests.first") - .filteredOn("name", "[2] value=b") + .filteredOn("name", "first(String) [2] value=b") .flatExtracting(TestResult::getParameters) .extracting(Parameter::getName, Parameter::getValue, Parameter::getMode, Parameter::getExcluded) - .containsExactlyInAnyOrder( + .contains( tuple("value", "b", null, null) ); @@ -86,20 +84,18 @@ void shouldSupportParamAnnotationForParameters() { final AllureResults results = runClasses(ParameterisedTests.class); assertThat(results.getTestResults()) - .filteredOn("fullName", "io.qameta.allure.junit5.features.ParameterisedTests.third") - .filteredOn("name", "[1] value=a") + .filteredOn("name", "third(String) [1] value=a") .flatExtracting(TestResult::getParameters) .extracting(Parameter::getName, Parameter::getValue, Parameter::getMode, Parameter::getExcluded) - .containsExactlyInAnyOrder( + .contains( tuple("some value", "a", Parameter.Mode.DEFAULT, false) ); assertThat(results.getTestResults()) - .filteredOn("fullName", "io.qameta.allure.junit5.features.ParameterisedTests.third") - .filteredOn("name", "[2] value=b") + .filteredOn("name", "third(String) [2] value=b") .flatExtracting(TestResult::getParameters) .extracting(Parameter::getName, Parameter::getValue, Parameter::getMode, Parameter::getExcluded) - .containsExactlyInAnyOrder( + .contains( tuple("some value", "b", Parameter.Mode.DEFAULT, false) ); @@ -122,19 +118,19 @@ void shouldSkipReportingOfTestInjectablesTestReporterForParameterisedTest() { assertThat(results.getTestResults()) .filteredOn("fullName", "io.qameta.allure.junit5.features.SkipOtherInjectables.testReporterInjection") - .filteredOn("name", "[1] value=a") + .filteredOn("name", "testReporterInjection(String, TestReporter) [1] value=a") .flatExtracting(TestResult::getParameters) .extracting(Parameter::getName, Parameter::getValue, Parameter::getMode, Parameter::getExcluded) - .containsExactlyInAnyOrder( + .contains( tuple("value", "a", null, null) ); assertThat(results.getTestResults()) .filteredOn("fullName", "io.qameta.allure.junit5.features.SkipOtherInjectables.testReporterInjection") - .filteredOn("name", "[2] value=b") + .filteredOn("name", "testReporterInjection(String, TestReporter) [2] value=b") .flatExtracting(TestResult::getParameters) .extracting(Parameter::getName, Parameter::getValue, Parameter::getMode, Parameter::getExcluded) - .containsExactlyInAnyOrder( + .contains( tuple("value", "b", null, null) ); } diff --git a/gradle/quality-configs/pmd/pmd.xml b/gradle/quality-configs/pmd/pmd.xml index 181a946aa..ad5ac393c 100644 --- a/gradle/quality-configs/pmd/pmd.xml +++ b/gradle/quality-configs/pmd/pmd.xml @@ -144,7 +144,9 @@ - + + +