diff --git a/build.gradle b/build.gradle index 7b15c08c..053e84bb 100644 --- a/build.gradle +++ b/build.gradle @@ -39,7 +39,7 @@ repositories { } dependencies { - api 'com.epam.reportportal:client-java:5.2.5' + api 'com.epam.reportportal:client-java:5.2.6' api 'com.google.code.findbugs:jsr305:3.0.2' implementation 'org.testng:testng:7.9.0' diff --git a/src/main/java/com/epam/reportportal/testng/TestNGService.java b/src/main/java/com/epam/reportportal/testng/TestNGService.java index a3983824..d23881ab 100644 --- a/src/main/java/com/epam/reportportal/testng/TestNGService.java +++ b/src/main/java/com/epam/reportportal/testng/TestNGService.java @@ -15,6 +15,7 @@ */ package com.epam.reportportal.testng; +import com.epam.reportportal.annotations.Description; import com.epam.reportportal.annotations.ParameterKey; import com.epam.reportportal.annotations.TestCaseId; import com.epam.reportportal.annotations.attribute.Attributes; @@ -720,6 +721,10 @@ protected String createStepName(ITestResult testResult) { * @return Test/Step Description being sent to ReportPortal */ protected String createStepDescription(ITestResult testResult) { + var methodDescriptionOptional = getMethodAnnotation(Description.class, testResult); + if(methodDescriptionOptional.isPresent()){ + return methodDescriptionOptional.get().value(); + } return testResult.getMethod().getDescription(); } diff --git a/src/test/java/com/epam/reportportal/testng/BuildStepTest.java b/src/test/java/com/epam/reportportal/testng/BuildStepTest.java index 995ced27..d3388809 100644 --- a/src/test/java/com/epam/reportportal/testng/BuildStepTest.java +++ b/src/test/java/com/epam/reportportal/testng/BuildStepTest.java @@ -16,6 +16,7 @@ package com.epam.reportportal.testng; +import com.epam.reportportal.annotations.Description; import com.epam.reportportal.annotations.ParameterKey; import com.epam.reportportal.annotations.TestCaseId; import com.epam.reportportal.annotations.TestCaseIdKey; @@ -43,7 +44,7 @@ import static org.hamcrest.Matchers.nullValue; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; /** * @author Pavel Bortnik @@ -64,9 +65,13 @@ public class BuildStepTest { @Mock private ConstructorOrMethod constructorOrMethod; + @Mock + private Description descriptionAnnotation; + @BeforeEach public void initMocks() { testNGService = new TestNGService(new MemoizingSupplier<>(() -> launch)); + descriptionAnnotation = mock(Description.class); } @Test @@ -89,18 +94,24 @@ public void testDescription() { assertThat("Incorrect test description", rq.getDescription(), is(DEFAULT_DESCRIPTION)); } + @Test + public void testAnnotatedDescriptionAnnotated() { + when(testResult.getMethod()).thenReturn(testNGMethod); + when(testNGMethod.getConstructorOrMethod()).thenReturn(constructorOrMethod); + when(testNGMethod.isTest()).thenReturn(true); + Method method = getTestMethodsExampleByName("testDescriptionAnnotation"); + when(constructorOrMethod.getMethod()).thenReturn(method); + StartTestItemRQ rq = testNGService.buildStartStepRq(testResult); + assertThat("Incorrect test description", rq.getDescription(), is(DEFAULT_ANNOTATION_DESCRIPTION)); + } + @Test public void testParameters() { when(testResult.getMethod()).thenReturn(testNGMethod); when(testNGMethod.getConstructorOrMethod()).thenReturn(constructorOrMethod); when(testNGMethod.isTest()).thenReturn(true); - Method[] methods = TestMethodsExamples.class.getDeclaredMethods(); - Method method = null; - for (Method m : methods) { - if (m.getName().contains("parametersAnnotation")) { - method = m; - } - } + Method method = getTestMethodsExampleByName("parametersAnnotation"); + when(constructorOrMethod.getMethod()).thenReturn(method); when(testResult.getParameters()).thenReturn(new Object[] { "param_0", "param_1" }); StartTestItemRQ rq = testNGService.buildStartStepRq(testResult); @@ -118,13 +129,7 @@ public void testParametersDataProvider_DefaultKey() { when(testResult.getMethod()).thenReturn(testNGMethod); when(testNGMethod.getConstructorOrMethod()).thenReturn(constructorOrMethod); when(testNGMethod.isTest()).thenReturn(true); - Method[] methods = TestMethodsExamples.class.getDeclaredMethods(); - Method method = null; - for (Method m : methods) { - if (m.getName().contains("dataProviderWithoutKey")) { - method = m; - } - } + Method method = getTestMethodsExampleByName("dataProviderWithoutKey"); when(constructorOrMethod.getMethod()).thenReturn(method); when(testResult.getParameters()).thenReturn(new Object[] { "param_0", "param_1" }); @@ -142,13 +147,7 @@ public void testParametersDataProvider_ProvidedKey() { when(testResult.getMethod()).thenReturn(testNGMethod); when(testNGMethod.getConstructorOrMethod()).thenReturn(constructorOrMethod); when(testNGMethod.isTest()).thenReturn(true); - Method[] methods = TestMethodsExamples.class.getDeclaredMethods(); - Method method = null; - for (Method m : methods) { - if (m.getName().contains("dataProviderWithParameterKey")) { - method = m; - } - } + Method method = getTestMethodsExampleByName("dataProviderWithParameterKey"); when(constructorOrMethod.getMethod()).thenReturn(method); when(testResult.getParameters()).thenReturn(new Object[] { "param_0", "param_1" }); @@ -300,13 +299,10 @@ public void testCaseId_fromAnnotation() { when(testResult.getMethod()).thenReturn(testNGMethod); when(testNGMethod.getConstructorOrMethod()).thenReturn(constructorOrMethod); when(testNGMethod.isTest()).thenReturn(true); - Optional methodOptional = Arrays.stream(TestMethodsExamples.class.getDeclaredMethods()) - .filter(it -> it.getName().equals("testCaseId")) - .findFirst(); - assertTrue(methodOptional.isPresent()); + Method method = getTestMethodsExampleByName("testCaseId"); String expectedCodeRef = "com.test.BuildStepTest.codeRefTest"; - when(constructorOrMethod.getMethod()).thenReturn(methodOptional.get()); + when(constructorOrMethod.getMethod()).thenReturn(method); when(testResult.getMethod().getQualifiedName()).thenReturn(expectedCodeRef); StartTestItemRQ request = testNGService.buildStartStepRq(testResult); @@ -320,14 +316,11 @@ public void testCaseId_fromAnnotationParametrized() { when(testResult.getMethod()).thenReturn(testNGMethod); when(testNGMethod.getConstructorOrMethod()).thenReturn(constructorOrMethod); when(testNGMethod.isTest()).thenReturn(true); - Optional methodOptional = Arrays.stream(TestMethodsExamples.class.getDeclaredMethods()) - .filter(it -> it.getName().equals("testCaseIdParameterized")) - .findFirst(); - assertTrue(methodOptional.isPresent()); + Method method = getTestMethodsExampleByName("testCaseIdParameterized"); String expectedCodeRef = "com.test.BuildStepTest.codeRefTest"; String expectedParam = "test-case-id-key"; - when(constructorOrMethod.getMethod()).thenReturn(methodOptional.get()); + when(constructorOrMethod.getMethod()).thenReturn(method); when(testResult.getMethod().getQualifiedName()).thenReturn(expectedCodeRef); when(testResult.getParameters()).thenReturn(new Object[] { expectedParam }); @@ -337,6 +330,14 @@ public void testCaseId_fromAnnotationParametrized() { assertEquals(expectedParam, request.getTestCaseId()); } + private Method getTestMethodsExampleByName(String methodName){ + Optional methodOptional = Arrays.stream(TestMethodsExamples.class.getDeclaredMethods()) + .filter(it -> it.getName().equals(methodName)) + .findFirst(); + assertTrue(methodOptional.isPresent()); + return methodOptional.get(); + } + private static class TestMethodsExamples { @Ignore @org.testng.annotations.Test(dataProvider = "dp") @@ -370,6 +371,11 @@ private void testCaseId() { private void testCaseIdParameterized(@TestCaseIdKey String param) { //just for testing providing annotation } + + @Description(DEFAULT_ANNOTATION_DESCRIPTION) + private void testDescriptionAnnotation() { + //just for testing providing annotation + } } } diff --git a/src/test/java/com/epam/reportportal/testng/Constants.java b/src/test/java/com/epam/reportportal/testng/Constants.java index f95d869c..02263e64 100644 --- a/src/test/java/com/epam/reportportal/testng/Constants.java +++ b/src/test/java/com/epam/reportportal/testng/Constants.java @@ -36,6 +36,7 @@ public class Constants { static final Set ATTRIBUTES = new HashSet<>(Collections.singleton(ATTRIBUTE)); static final Mode MODE = Mode.DEFAULT; static final String DEFAULT_DESCRIPTION = "default_description"; + static final String DEFAULT_ANNOTATION_DESCRIPTION = "default_annotation_description"; static final Long DEFAULT_TIME = 1515594836210L; } diff --git a/src/test/java/com/epam/reportportal/testng/TestNameAndDescriptionTest.java b/src/test/java/com/epam/reportportal/testng/TestNameAndDescriptionTest.java index df7ed20c..68f58f2c 100644 --- a/src/test/java/com/epam/reportportal/testng/TestNameAndDescriptionTest.java +++ b/src/test/java/com/epam/reportportal/testng/TestNameAndDescriptionTest.java @@ -3,6 +3,8 @@ import com.epam.reportportal.service.ReportPortal; import com.epam.reportportal.service.ReportPortalClient; import com.epam.reportportal.testng.integration.TestNgListener; +import com.epam.reportportal.testng.integration.feature.description.DescriptionAnnotatedAndTestNgDescriptionTest; +import com.epam.reportportal.testng.integration.feature.description.DescriptionAnnotatedTest; import com.epam.reportportal.testng.integration.feature.description.DescriptionTest; import com.epam.reportportal.testng.integration.feature.name.AnnotationNamedClassTest; import com.epam.reportportal.testng.integration.feature.name.AnnotationNamedParameterizedClassTest; @@ -70,6 +72,36 @@ public void test_description_should_be_passed_to_rp_if_specified() { assertThat(startItem.getDescription(), equalTo(DescriptionTest.TEST_DESCRIPTION)); } + @Test + public void test_annotation_description_should_be_passed_to_rp_if_description_annotation_is_specified() { + runTests(Collections.singletonList(TestNgListener.class), DescriptionAnnotatedTest.class); + + verify(client, times(1)).startLaunch(any()); // Start launch + verify(client, times(1)).startTestItem(any()); // Start parent suites + verify(client, times(1)).startTestItem(same(suitedUuid), any()); // Start test class + + ArgumentCaptor startTestCapture = ArgumentCaptor.forClass(StartTestItemRQ.class); + verify(client, times(1)).startTestItem(same(testClassUuid), startTestCapture.capture()); + StartTestItemRQ startItem = startTestCapture.getAllValues().get(0); + + assertThat(startItem.getDescription(), equalTo(DescriptionAnnotatedTest.TEST_DESCRIPTION_ANNOTATION)); + } + + @Test + public void test_annotation_description_should_be_passed_to_rp_if_both_test_description_and_description_annotation_are_specified() { + runTests(Collections.singletonList(TestNgListener.class), DescriptionAnnotatedAndTestNgDescriptionTest.class); + + verify(client, times(1)).startLaunch(any()); // Start launch + verify(client, times(1)).startTestItem(any()); // Start parent suites + verify(client, times(1)).startTestItem(same(suitedUuid), any()); // Start test class + + ArgumentCaptor startTestCapture = ArgumentCaptor.forClass(StartTestItemRQ.class); + verify(client, times(1)).startTestItem(same(testClassUuid), startTestCapture.capture()); + StartTestItemRQ startItem = startTestCapture.getAllValues().get(0); + + assertThat(startItem.getDescription(), equalTo(DescriptionAnnotatedTest.TEST_DESCRIPTION_ANNOTATION)); + } + @Test public void test_name_should_be_passed_to_rp_if_specified_parameterized_test() { runTests(Collections.singletonList(TestNgListener.class), AnnotationNamedParameterizedClassTest.class); diff --git a/src/test/java/com/epam/reportportal/testng/integration/feature/description/DescriptionAnnotatedAndTestNgDescriptionTest.java b/src/test/java/com/epam/reportportal/testng/integration/feature/description/DescriptionAnnotatedAndTestNgDescriptionTest.java new file mode 100644 index 00000000..f5985963 --- /dev/null +++ b/src/test/java/com/epam/reportportal/testng/integration/feature/description/DescriptionAnnotatedAndTestNgDescriptionTest.java @@ -0,0 +1,17 @@ +package com.epam.reportportal.testng.integration.feature.description; + +import com.epam.reportportal.annotations.Description; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.annotations.Test; + +public class DescriptionAnnotatedAndTestNgDescriptionTest { + private static final Logger LOGGER = LoggerFactory.getLogger(DescriptionAnnotatedAndTestNgDescriptionTest.class); + + @Test(description = DescriptionTest.TEST_DESCRIPTION) + @Description(DescriptionAnnotatedTest.TEST_DESCRIPTION_ANNOTATION) + public void testDescriptionAnnotatedAndTestNgDescriptionTest() { + LOGGER.info("Inside 'testDescriptionAnnotatedAndTestNgDescriptionTest' method"); + } + +} diff --git a/src/test/java/com/epam/reportportal/testng/integration/feature/description/DescriptionAnnotatedTest.java b/src/test/java/com/epam/reportportal/testng/integration/feature/description/DescriptionAnnotatedTest.java new file mode 100644 index 00000000..eff4fb20 --- /dev/null +++ b/src/test/java/com/epam/reportportal/testng/integration/feature/description/DescriptionAnnotatedTest.java @@ -0,0 +1,19 @@ +package com.epam.reportportal.testng.integration.feature.description; + +import com.epam.reportportal.annotations.Description; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.annotations.Test; + +public class DescriptionAnnotatedTest { + private static final Logger LOGGER = LoggerFactory.getLogger(DescriptionAnnotatedTest.class); + + public static final String TEST_DESCRIPTION_ANNOTATION = "My annotated test description"; + + @Test + @Description(TEST_DESCRIPTION_ANNOTATION) + public void testDescriptionAnnotatedTest() { + LOGGER.info("Inside 'testDescriptionAnnotatedTest' method"); + } + +}