Skip to content

Commit

Permalink
Merge pull request #207 from oleksandr-fomenko/RPP_Agents_Java_TestNG…
Browse files Browse the repository at this point in the history
…_Annotation_Description

Added support of the @description annotation
  • Loading branch information
HardNorth committed Mar 19, 2024
2 parents 00763ff + 54db436 commit c1f2bb6
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 33 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/epam/reportportal/testng/TestNGService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
70 changes: 38 additions & 32 deletions src/test/java/com/epam/reportportal/testng/BuildStepTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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" });
Expand All @@ -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" });
Expand Down Expand Up @@ -300,13 +299,10 @@ public void testCaseId_fromAnnotation() {
when(testResult.getMethod()).thenReturn(testNGMethod);
when(testNGMethod.getConstructorOrMethod()).thenReturn(constructorOrMethod);
when(testNGMethod.isTest()).thenReturn(true);
Optional<Method> 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);
Expand All @@ -320,14 +316,11 @@ public void testCaseId_fromAnnotationParametrized() {
when(testResult.getMethod()).thenReturn(testNGMethod);
when(testNGMethod.getConstructorOrMethod()).thenReturn(constructorOrMethod);
when(testNGMethod.isTest()).thenReturn(true);
Optional<Method> 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 });

Expand All @@ -337,6 +330,14 @@ public void testCaseId_fromAnnotationParametrized() {
assertEquals(expectedParam, request.getTestCaseId());
}

private Method getTestMethodsExampleByName(String methodName){
Optional<Method> 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")
Expand Down Expand Up @@ -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
}
}

}
1 change: 1 addition & 0 deletions src/test/java/com/epam/reportportal/testng/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class Constants {
static final Set<ItemAttributesRQ> 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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<StartTestItemRQ> 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<StartTestItemRQ> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}

}
Original file line number Diff line number Diff line change
@@ -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");
}

}

0 comments on commit c1f2bb6

Please sign in to comment.