From e19178f2cc9b677064c261cc92de8f53654ad782 Mon Sep 17 00:00:00 2001 From: Sambhav Dave Date: Sun, 22 Sep 2024 12:00:40 +0530 Subject: [PATCH] Fix for #893 - param annotation name not coming up in report --- .../io/qameta/allure/testng/AllureTestNg.java | 26 +++++++-- .../allure/testng/AllureTestNgTest.java | 36 ++++++------ .../samples/CustomParameterNamesTest.java | 57 +++++++++++++++++++ .../testng/samples/ParameterizedTest.java | 8 +-- .../src/test/resources/suites/gh-893.xml | 10 ++++ .../resources/suites/parameterized-test.xml | 3 +- 6 files changed, 109 insertions(+), 31 deletions(-) create mode 100644 allure-testng/src/test/java/io/qameta/allure/testng/samples/CustomParameterNamesTest.java create mode 100644 allure-testng/src/test/resources/suites/gh-893.xml diff --git a/allure-testng/src/main/java/io/qameta/allure/testng/AllureTestNg.java b/allure-testng/src/main/java/io/qameta/allure/testng/AllureTestNg.java index ebbc0a63e..db318072b 100644 --- a/allure-testng/src/main/java/io/qameta/allure/testng/AllureTestNg.java +++ b/allure-testng/src/main/java/io/qameta/allure/testng/AllureTestNg.java @@ -21,6 +21,7 @@ import io.qameta.allure.Muted; import io.qameta.allure.Severity; import io.qameta.allure.SeverityLevel; +import io.qameta.allure.Param; import io.qameta.allure.model.FixtureResult; import io.qameta.allure.model.Label; import io.qameta.allure.model.Link; @@ -74,6 +75,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.function.Consumer; import java.util.stream.Collectors; +import java.util.stream.IntStream; import java.util.stream.Stream; import static io.qameta.allure.util.ResultsUtils.ALLURE_ID_LABEL_NAME; @@ -779,9 +781,23 @@ private List getParameters(final ITestContext context, .map(Parameters::value) .orElse(new String[]{}); - final String[] reflectionNames = Stream.of(m.getParameters()) - .map(java.lang.reflect.Parameter::getName) - .toArray(String[]::new); + final List reflectionNames = IntStream + .range(0, parameters.length) + .mapToObj(index -> { + final Parameter parameter = createParameter(m.getParameters()[index].getName(), parameters[index]); + Stream.of(m.getParameters()[index].getAnnotationsByType(Param.class)) + .findFirst() + .ifPresent(param -> { + Stream.of(param.name().trim()) + .map(String::trim) + .filter(name -> !name.isEmpty()) + .findFirst() + .ifPresent(parameter::setName); + parameter.setMode(param.mode()); + parameter.setExcluded(param.excluded()); + }); + return parameter; + }).collect(Collectors.toList()); int skippedCount = 0; for (int i = 0; i < parameterTypes.length; i++) { @@ -797,8 +813,8 @@ private List getParameters(final ITestContext context, continue; } - if (i < reflectionNames.length) { - result.put(reflectionNames[i], ObjectUtils.toString(parameters[i])); + if (i < reflectionNames.size()) { + result.put(reflectionNames.get(i).getName(), ObjectUtils.toString(parameters[i])); } } diff --git a/allure-testng/src/test/java/io/qameta/allure/testng/AllureTestNgTest.java b/allure-testng/src/test/java/io/qameta/allure/testng/AllureTestNgTest.java index 7af9c5303..34b34252e 100644 --- a/allure-testng/src/test/java/io/qameta/allure/testng/AllureTestNgTest.java +++ b/allure-testng/src/test/java/io/qameta/allure/testng/AllureTestNgTest.java @@ -17,18 +17,7 @@ import io.qameta.allure.Issue; import io.qameta.allure.Step; -import io.qameta.allure.model.Attachment; -import io.qameta.allure.model.FixtureResult; -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; -import io.qameta.allure.model.StepResult; -import io.qameta.allure.model.TestResult; -import io.qameta.allure.model.TestResultContainer; -import io.qameta.allure.model.WithSteps; +import io.qameta.allure.model.*; import io.qameta.allure.test.AllureFeatures; import io.qameta.allure.test.AllureResults; import io.qameta.allure.test.RunUtils; @@ -46,12 +35,7 @@ import org.testng.xml.XmlSuite; import java.net.URL; -import java.util.Arrays; -import java.util.Collection; -import java.util.Comparator; -import java.util.List; -import java.util.Objects; -import java.util.Optional; +import java.util.*; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -1198,6 +1182,22 @@ public void shouldSupportFactoryOnConstructor() { ); } + @SuppressWarnings("unchecked") + @AllureFeatures.Parameters + @Issue("893") + @Test + public void shouldDisplayCustomNamesOfParameters() { + final AllureResults results = runTestNgSuites("suites/gh-893.xml"); + assertThat(results.getTestResults()) + .flatExtracting(TestResult::getParameters) + .extracting(Parameter::getName, Parameter::getValue) + .containsExactlyInAnyOrder( + tuple("First", "1"), + tuple("Second", "1"), + tuple("Third", "2"), + tuple("Fourth", "5")); + } + @DataProvider(name = "failedFixtures") public Object[][] failedFixtures() { return new Object[][]{ diff --git a/allure-testng/src/test/java/io/qameta/allure/testng/samples/CustomParameterNamesTest.java b/allure-testng/src/test/java/io/qameta/allure/testng/samples/CustomParameterNamesTest.java new file mode 100644 index 000000000..ffdffadba --- /dev/null +++ b/allure-testng/src/test/java/io/qameta/allure/testng/samples/CustomParameterNamesTest.java @@ -0,0 +1,57 @@ +/* + * Copyright 2016-2024 Qameta Software Inc + * + * 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.testng.samples; + +import io.qameta.allure.Param; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import static io.qameta.allure.Allure.step; + +/** + * @author Sambhav Dave sambhavd4@gmail.com + */ +public class CustomParameterNamesTest { + + @DataProvider + public static Object[][] testDataForParamNames() { + return new Object[][]{ + {1, 1, 2, 5} + }; + } + + @Test(dataProvider = "testDataForParamNames") + public void sumTest( + @Param(name = "First") Integer a, + @Param(name = "Second") Integer b, + @Param(name = "Third") Integer expectedSum, + @Param(name = "Fourth") Integer unusedParam) { + + step("Arrange", () -> step(String.format("Use parameters: First = [%s], Second = [%s], Third = [%s], Fourth = [%s]", + a, b, expectedSum, unusedParam))); + + Integer result = step("Act", () -> { + step(String.format("Add First [%s] and Second [%s]", a, b)); + return a + b; + }); + + step("Assert", () -> { + step(String.format("Compare result [%s] with expected [%s]", result, expectedSum)); + assert result.equals(expectedSum) : "Sum does not match the expected value"; + }); + } + +} diff --git a/allure-testng/src/test/java/io/qameta/allure/testng/samples/ParameterizedTest.java b/allure-testng/src/test/java/io/qameta/allure/testng/samples/ParameterizedTest.java index 385689b14..05ab6b268 100644 --- a/allure-testng/src/test/java/io/qameta/allure/testng/samples/ParameterizedTest.java +++ b/allure-testng/src/test/java/io/qameta/allure/testng/samples/ParameterizedTest.java @@ -15,11 +15,12 @@ */ package io.qameta.allure.testng.samples; -import io.qameta.allure.Step; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; +import static io.qameta.allure.Allure.step; + /** * @author Egor Borisov ehborisov@gmail.com */ @@ -42,9 +43,4 @@ public Object[][] testData() { public void parameterizedTest(String param) { step(param); } - - @Step - public void step(String param) { - - } } diff --git a/allure-testng/src/test/resources/suites/gh-893.xml b/allure-testng/src/test/resources/suites/gh-893.xml new file mode 100644 index 000000000..9d4ae9ed0 --- /dev/null +++ b/allure-testng/src/test/resources/suites/gh-893.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/allure-testng/src/test/resources/suites/parameterized-test.xml b/allure-testng/src/test/resources/suites/parameterized-test.xml index 6911eb613..a4761200b 100644 --- a/allure-testng/src/test/resources/suites/parameterized-test.xml +++ b/allure-testng/src/test/resources/suites/parameterized-test.xml @@ -4,8 +4,7 @@ - - +