From 90adc1e2767a2b28424733837dd0ec9129a54c1d Mon Sep 17 00:00:00 2001 From: Aliaksandr Rasolka Date: Mon, 7 Apr 2025 19:42:49 +0300 Subject: [PATCH] [TestNG] Lookup test name for report first in 'testName' field inside 'Test' annotation --- .../io/qameta/allure/testng/AllureTestNg.java | 21 +++++++++++++++++++ .../allure/testng/AllureTestNgTest.java | 6 +++++- .../testng/samples/CyrillicDescriptions.java | 11 +++++++++- 3 files changed, 36 insertions(+), 2 deletions(-) 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 ebbc0a63..18224029 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 @@ -51,6 +51,7 @@ import org.testng.ITestNGMethod; import org.testng.ITestResult; import org.testng.annotations.Parameters; +import org.testng.annotations.Test; import org.testng.internal.ConstructorOrMethod; import org.testng.xml.XmlSuite; import org.testng.xml.XmlTest; @@ -811,6 +812,7 @@ private List getParameters(final ITestContext context, private String getMethodName(final ITestNGMethod method) { return firstNonEmpty( + getTestNameFromAnnotation(method), method.getDescription(), method.getMethodName(), getQualifiedName(method)).orElse("Unknown"); @@ -881,6 +883,25 @@ private static boolean isClassAvailableOnClasspath(final String clazz) { } } + private String getTestNameFromAnnotation(final ITestNGMethod iTestNGMethod) { + final ConstructorOrMethod constructorOrMethod = iTestNGMethod.getConstructorOrMethod(); + if (Objects.isNull(constructorOrMethod)) { + return null; + } + + final Method method = constructorOrMethod.getMethod(); + if (Objects.isNull(method)) { + return null; + } + + final Test annotation = method.getAnnotation(Test.class); + if (Objects.isNull(annotation)) { + return null; + } + + return annotation.testName(); + } + /** * The stage of current result context. */ 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 7af9c530..f7d4c87b 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 @@ -997,7 +997,11 @@ public void shouldProcessCyrillicDescriptions() { assertThat(results.getTestResults()) .extracting(TestResult::getName) - .containsExactlyInAnyOrder("Тест с описанием на русском языке"); + .contains( + "Тест с описанием на русском языке только в testName", + "Тест с описанием на русском языке только в description", + "Тест с описанием на русском языке и в testName" + ); } @AllureFeatures.Fixtures diff --git a/allure-testng/src/test/java/io/qameta/allure/testng/samples/CyrillicDescriptions.java b/allure-testng/src/test/java/io/qameta/allure/testng/samples/CyrillicDescriptions.java index 475ffb5b..8418a3fd 100644 --- a/allure-testng/src/test/java/io/qameta/allure/testng/samples/CyrillicDescriptions.java +++ b/allure-testng/src/test/java/io/qameta/allure/testng/samples/CyrillicDescriptions.java @@ -22,7 +22,16 @@ */ public class CyrillicDescriptions { - @Test(description = "Тест с описанием на русском языке") + @Test(testName = "Тест с описанием на русском языке только в testName") + public void testWithCyrillicTestName() throws Exception { + } + + @Test(description = "Тест с описанием на русском языке только в description") public void testWithCyrillicDescription() throws Exception { } + + @Test(testName = "Тест с описанием на русском языке и в testName", + description = "Тест с описанием на русском языке и в description") + public void testWithCyrillicTestNameAndDescription() throws Exception { + } }