Skip to content

Commit 603bd6f

Browse files
committed
Fix for #893 - param annotation name not coming up in report
1 parent 3d361dd commit 603bd6f

File tree

6 files changed

+107
-13
lines changed

6 files changed

+107
-13
lines changed

allure-testng/src/main/java/io/qameta/allure/testng/AllureTestNg.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.qameta.allure.Muted;
2222
import io.qameta.allure.Severity;
2323
import io.qameta.allure.SeverityLevel;
24+
import io.qameta.allure.Param;
2425
import io.qameta.allure.model.FixtureResult;
2526
import io.qameta.allure.model.Label;
2627
import io.qameta.allure.model.Link;
@@ -74,6 +75,7 @@
7475
import java.util.concurrent.locks.ReentrantReadWriteLock;
7576
import java.util.function.Consumer;
7677
import java.util.stream.Collectors;
78+
import java.util.stream.IntStream;
7779
import java.util.stream.Stream;
7880

7981
import static io.qameta.allure.util.ResultsUtils.ALLURE_ID_LABEL_NAME;
@@ -779,9 +781,23 @@ private List<Parameter> getParameters(final ITestContext context,
779781
.map(Parameters::value)
780782
.orElse(new String[]{});
781783

782-
final String[] reflectionNames = Stream.of(m.getParameters())
783-
.map(java.lang.reflect.Parameter::getName)
784-
.toArray(String[]::new);
784+
final List<Parameter> reflectionNames = IntStream
785+
.range(0, parameters.length)
786+
.mapToObj(index -> {
787+
final Parameter parameter = createParameter(m.getParameters()[index].getName(), parameters[index]);
788+
Stream.of(m.getParameters()[index].getAnnotationsByType(Param.class))
789+
.findFirst()
790+
.ifPresent(param -> {
791+
Stream.of(param.name().trim())
792+
.map(String::trim)
793+
.filter(name -> !name.isEmpty())
794+
.findFirst()
795+
.ifPresent(parameter::setName);
796+
parameter.setMode(param.mode());
797+
parameter.setExcluded(param.excluded());
798+
});
799+
return parameter;
800+
}).collect(Collectors.toList());
785801

786802
int skippedCount = 0;
787803
for (int i = 0; i < parameterTypes.length; i++) {
@@ -797,8 +813,8 @@ private List<Parameter> getParameters(final ITestContext context,
797813
continue;
798814
}
799815

800-
if (i < reflectionNames.length) {
801-
result.put(reflectionNames[i], ObjectUtils.toString(parameters[i]));
816+
if (i < reflectionNames.size()) {
817+
result.put(reflectionNames.get(i).getName(), ObjectUtils.toString(parameters[i]));
802818
}
803819
}
804820

allure-testng/src/test/java/io/qameta/allure/testng/AllureTestNgTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,22 @@ public void shouldSupportFactoryOnConstructor() {
11981198
);
11991199
}
12001200

1201+
@SuppressWarnings("unchecked")
1202+
@AllureFeatures.Parameters
1203+
@Issue("893")
1204+
@Test
1205+
public void shouldDisplayCustomNamesOfParameters() {
1206+
final AllureResults results = runTestNgSuites("suites/gh-893.xml");
1207+
assertThat(results.getTestResults())
1208+
.flatExtracting(TestResult::getParameters)
1209+
.extracting(Parameter::getName, Parameter::getValue)
1210+
.containsExactlyInAnyOrder(
1211+
tuple("First", "1"),
1212+
tuple("Second", "1"),
1213+
tuple("Third", "2"),
1214+
tuple("Fourth", "5"));
1215+
}
1216+
12011217
@DataProvider(name = "failedFixtures")
12021218
public Object[][] failedFixtures() {
12031219
return new Object[][]{
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2016-2024 Qameta Software Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.qameta.allure.testng.samples;
17+
18+
import io.qameta.allure.Param;
19+
import org.testng.annotations.DataProvider;
20+
import org.testng.annotations.Test;
21+
22+
import static io.qameta.allure.Allure.step;
23+
24+
/**
25+
* @author Sambhav Dave [email protected]
26+
*/
27+
public class CustomParameterNamesTest {
28+
29+
@DataProvider
30+
public static Object[][] testDataForParamNames() {
31+
return new Object[][]{
32+
{1, 1, 2, 5}
33+
};
34+
}
35+
36+
@Test(dataProvider = "testDataForParamNames")
37+
public void sumTest(
38+
@Param(name = "First") Integer a,
39+
@Param(name = "Second") Integer b,
40+
@Param(name = "Third") Integer expectedSum,
41+
@Param(name = "Fourth") Integer unusedParam) {
42+
43+
step("Arrange", () -> step(String.format("Use parameters: First = [%s], Second = [%s], Third = [%s], Fourth = [%s]",
44+
a, b, expectedSum, unusedParam)));
45+
46+
Integer result = step("Act", () -> {
47+
step(String.format("Add First [%s] and Second [%s]", a, b));
48+
return a + b;
49+
});
50+
51+
step("Assert", () -> {
52+
step(String.format("Compare result [%s] with expected [%s]", result, expectedSum));
53+
assert result.equals(expectedSum) : "Sum does not match the expected value";
54+
});
55+
}
56+
57+
}

allure-testng/src/test/java/io/qameta/allure/testng/samples/ParameterizedTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
*/
1616
package io.qameta.allure.testng.samples;
1717

18-
import io.qameta.allure.Step;
1918
import org.testng.annotations.BeforeMethod;
2019
import org.testng.annotations.DataProvider;
2120
import org.testng.annotations.Test;
2221

22+
import static io.qameta.allure.Allure.step;
23+
2324
/**
2425
* @author Egor Borisov [email protected]
2526
*/
@@ -42,9 +43,4 @@ public Object[][] testData() {
4243
public void parameterizedTest(String param) {
4344
step(param);
4445
}
45-
46-
@Step
47-
public void step(String param) {
48-
49-
}
5046
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
3+
4+
<suite name="Github Issues">
5+
<test name="gh-893">
6+
<classes>
7+
<class name="io.qameta.allure.testng.samples.CustomParameterNamesTest"/>
8+
</classes>
9+
</test>
10+
</suite>

allure-testng/src/test/resources/suites/parameterized-test.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
<suite name="Test suite 6">
55
<test name="Test tag 6">
66
<classes>
7-
<class name="io.qameta.allure.testng.samples.ParameterizedTest">
8-
</class>
7+
<class name="io.qameta.allure.testng.samples.ParameterizedTest"/>
98
</classes>
109
</test>
1110
</suite>

0 commit comments

Comments
 (0)