Skip to content

Commit

Permalink
Fix for #893 - param annotation name not coming up in report
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-possible committed Sep 25, 2024
1 parent 3d361dd commit 603bd6f
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -779,9 +781,23 @@ private List<Parameter> 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<Parameter> 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++) {
Expand All @@ -797,8 +813,8 @@ private List<Parameter> 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]));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,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[][]{
Expand Down
Original file line number Diff line number Diff line change
@@ -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 [email protected]
*/
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";
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]
*/
Expand All @@ -42,9 +43,4 @@ public Object[][] testData() {
public void parameterizedTest(String param) {
step(param);
}

@Step
public void step(String param) {

}
}
10 changes: 10 additions & 0 deletions allure-testng/src/test/resources/suites/gh-893.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Github Issues">
<test name="gh-893">
<classes>
<class name="io.qameta.allure.testng.samples.CustomParameterNamesTest"/>
</classes>
</test>
</suite>
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
<suite name="Test suite 6">
<test name="Test tag 6">
<classes>
<class name="io.qameta.allure.testng.samples.ParameterizedTest">
</class>
<class name="io.qameta.allure.testng.samples.ParameterizedTest"/>
</classes>
</test>
</suite>

0 comments on commit 603bd6f

Please sign in to comment.