Skip to content

Commit

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

Added support of the @DisplayName annotation
  • Loading branch information
HardNorth authored Mar 27, 2024
2 parents 760b217 + 1c1a5e0 commit 60bb124
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 4 deletions.
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 @@ -16,6 +16,7 @@
package com.epam.reportportal.testng;

import com.epam.reportportal.annotations.Description;
import com.epam.reportportal.annotations.DisplayName;
import com.epam.reportportal.annotations.ParameterKey;
import com.epam.reportportal.annotations.TestCaseId;
import com.epam.reportportal.annotations.attribute.Attributes;
Expand Down Expand Up @@ -710,6 +711,10 @@ protected String createConfigurationDescription(ITestResult testResult) {
* @return Test/Step Name being sent to ReportPortal
*/
protected String createStepName(ITestResult testResult) {
var methodDisplayNameOptional = getMethodAnnotation(DisplayName.class, testResult);
if(methodDisplayNameOptional.isPresent()){
return methodDisplayNameOptional.get().value();
}
String testStepName = testResult.getTestName();
return testStepName == null ? testResult.getMethod().getMethodName() : testStepName;
}
Expand Down
21 changes: 17 additions & 4 deletions src/test/java/com/epam/reportportal/testng/BuildStepTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.epam.reportportal.testng;

import com.epam.reportportal.annotations.Description;
import com.epam.reportportal.annotations.DisplayName;
import com.epam.reportportal.annotations.ParameterKey;
import com.epam.reportportal.annotations.TestCaseId;
import com.epam.reportportal.annotations.TestCaseIdKey;
Expand Down Expand Up @@ -65,13 +66,9 @@ 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 @@ -84,6 +81,17 @@ public void testMethodName() {
assertThat("Incorrect test item name", rq.getName(), is(DEFAULT_NAME));
}

@Test
public void testMethodNameDisplayNameAnnotated() {
when(testResult.getMethod()).thenReturn(testNGMethod);
when(testNGMethod.getConstructorOrMethod()).thenReturn(constructorOrMethod);
when(testNGMethod.isTest()).thenReturn(true);
Method method = getTestMethodsExampleByName("testDisplayNameAnnotation");
when(constructorOrMethod.getMethod()).thenReturn(method);
StartTestItemRQ rq = testNGService.buildStartStepRq(testResult);
assertThat("Incorrect test item name", rq.getName(), is(DEFAULT_NAME_DISPLAY_NAME_ANNOTATION));
}

@Test
public void testDescription() {
when(testResult.getMethod()).thenReturn(testNGMethod);
Expand Down Expand Up @@ -376,6 +384,11 @@ private void testCaseIdParameterized(@TestCaseIdKey String param) {
private void testDescriptionAnnotation() {
//just for testing providing annotation
}

@DisplayName(DEFAULT_NAME_DISPLAY_NAME_ANNOTATION)
private void testDisplayNameAnnotation() {
//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 @@ -31,6 +31,7 @@ public class Constants {
static final String BASIC_URL = "basic_url";
static final String DEFAULT_UUID = "default_uuid";
static final String DEFAULT_NAME = "default_name";
static final String DEFAULT_NAME_DISPLAY_NAME_ANNOTATION = "default_display_name";
static final String DEFAULT_PROJECT = "default_project";
static final ItemAttributesRQ ATTRIBUTE = new ItemAttributesRQ("key", "value");
static final Set<ItemAttributesRQ> ATTRIBUTES = new HashSet<>(Collections.singleton(ATTRIBUTE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
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;
import com.epam.reportportal.testng.integration.feature.name.AnnotationNamedTestAndDisplayNameMethodClassTest;
import com.epam.reportportal.testng.integration.feature.name.AnnotationNamedDisplayNameMethodClassTest;
import com.epam.reportportal.testng.integration.feature.name.AnnotationNamedDisplayNameParameterizedClassTest;;
import com.epam.ta.reportportal.ws.model.StartTestItemRQ;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -57,6 +60,36 @@ public void test_name_should_be_passed_to_rp_if_specified() {
assertThat(startItem.getName(), equalTo(AnnotationNamedClassTest.TEST_NAME));
}

@Test
public void test_name_should_be_passed_to_rp_if_display_name_specified() {
runTests(Collections.singletonList(TestNgListener.class), AnnotationNamedDisplayNameMethodClassTest.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.getName(), equalTo(AnnotationNamedDisplayNameMethodClassTest.TEST_NAME_DISPLAY));
}

@Test
public void test_name_should_be_passed_to_rp_if_both_test_name_and_display_name_are_specified() {
runTests(Collections.singletonList(TestNgListener.class), AnnotationNamedTestAndDisplayNameMethodClassTest.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.getName(), equalTo(AnnotationNamedDisplayNameMethodClassTest.TEST_NAME_DISPLAY));
}

@Test
public void test_description_should_be_passed_to_rp_if_specified() {
runTests(Collections.singletonList(TestNgListener.class), DescriptionTest.class);
Expand Down Expand Up @@ -114,4 +147,20 @@ public void test_name_should_be_passed_to_rp_if_specified_parameterized_test() {

assertThat(startTestCapture.getValue().getName(), equalTo(AnnotationNamedParameterizedClassTest.TEST_NAME));
}

@Test
public void test_name_should_be_passed_to_rp_if_specified_parameterized_test_with_display_name_annotation() {
runTests(Collections.singletonList(TestNgListener.class), AnnotationNamedDisplayNameParameterizedClassTest.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(2)).startTestItem(same(testClassUuid), startTestCapture.capture());

startTestCapture.getAllValues().forEach(startItem -> {
assertThat(startItem.getName(), equalTo(AnnotationNamedDisplayNameMethodClassTest.TEST_NAME_DISPLAY));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.epam.reportportal.testng.integration.feature.name;

import com.epam.reportportal.annotations.DisplayName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;

public class AnnotationNamedDisplayNameMethodClassTest {
private static final Logger LOGGER = LoggerFactory.getLogger(AnnotationNamedDisplayNameMethodClassTest.class);

public static final String TEST_NAME_DISPLAY = "My display test name";

@Test
@DisplayName(TEST_NAME_DISPLAY)
public void testNameDisplayNameMethodTest() {
LOGGER.info("Inside 'testNameDisplayNameMethodTest' method");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.epam.reportportal.testng.integration.feature.name;

import com.epam.reportportal.annotations.DisplayName;
import com.ibm.icu.text.RuleBasedNumberFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.Locale;

import static com.epam.reportportal.testng.integration.feature.name.AnnotationNamedParameterizedClassTest.TEST_NAME;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

@Test(testName = TEST_NAME)
public class AnnotationNamedDisplayNameParameterizedClassTest {
private static final Logger LOGGER = LoggerFactory.getLogger(AnnotationNamedDisplayNameParameterizedClassTest.class);

@DataProvider(name = "provider")
public static Object[][] getData() {
return new Object[][] { new Object[] { 1, "one" }, new Object[] { 2, "two" } };
}

@Test(dataProvider = "provider")
@DisplayName(AnnotationNamedDisplayNameMethodClassTest.TEST_NAME_DISPLAY)
public void testNumberConversionDisplayName(int num, String numWord) {
LOGGER.info("Number: " + num + "; expected result: " + numWord);
Locale us = Locale.US;
RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(us, RuleBasedNumberFormat.SPELLOUT);
String result = formatter.format(num, "%spellout-numbering");
assertThat(result, equalTo(numWord));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.epam.reportportal.testng.integration.feature.name;

import com.epam.reportportal.annotations.DisplayName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;

@Test(testName = "Name from test annotation")
public class AnnotationNamedTestAndDisplayNameMethodClassTest {
private static final Logger LOGGER = LoggerFactory.getLogger(AnnotationNamedTestAndDisplayNameMethodClassTest.class);

@Test
@DisplayName(AnnotationNamedDisplayNameMethodClassTest.TEST_NAME_DISPLAY)
public void testNameDisplayNameMethodTest() {
LOGGER.info("Inside 'testNameDisplayNameMethodTest' method");
}

}

0 comments on commit 60bb124

Please sign in to comment.