Search in sources :

Example 1 with TestCaseId

use of com.epam.reportportal.annotations.TestCaseId in project agent-java-testNG by reportportal.

the class BuildStepTest method testCaseId_fromAnnotation.

@Test
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());
    String expectedCodeRef = "com.test.BuildStepTest.codeRefTest";
    when(constructorOrMethod.getMethod()).thenReturn(methodOptional.get());
    when(testResult.getMethod().getQualifiedName()).thenReturn(expectedCodeRef);
    StartTestItemRQ request = testNGService.buildStartStepRq(testResult);
    assertEquals(expectedCodeRef, request.getCodeRef());
    assertEquals("test-case-id", request.getTestCaseId());
}
Also used : Ignore(org.testng.annotations.Ignore) BeforeEach(org.junit.jupiter.api.BeforeEach) java.util(java.util) DataProvider(org.testng.annotations.DataProvider) TestCaseIdKey(com.epam.reportportal.annotations.TestCaseIdKey) Launch(com.epam.reportportal.service.Launch) Mock(org.mockito.Mock) TestCaseId(com.epam.reportportal.annotations.TestCaseId) ITestResult(org.testng.ITestResult) Constants(com.epam.reportportal.testng.Constants) FinishTestItemRQ(com.epam.ta.reportportal.ws.model.FinishTestItemRQ) StartTestItemRQ(com.epam.ta.reportportal.ws.model.StartTestItemRQ) Matchers.nullValue(org.hamcrest.Matchers.nullValue) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Method(java.lang.reflect.Method) MemoizingSupplier(com.epam.reportportal.utils.MemoizingSupplier) ItemStatus(com.epam.reportportal.listeners.ItemStatus) Mockito.when(org.mockito.Mockito.when) ParameterKey(com.epam.reportportal.annotations.ParameterKey) Test(org.junit.jupiter.api.Test) UniqueID(com.epam.reportportal.annotations.UniqueID) ITestNGMethod(org.testng.ITestNGMethod) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Parameters(org.testng.annotations.Parameters) Matchers.is(org.hamcrest.Matchers.is) ConstructorOrMethod(org.testng.internal.ConstructorOrMethod) Method(java.lang.reflect.Method) ITestNGMethod(org.testng.ITestNGMethod) ConstructorOrMethod(org.testng.internal.ConstructorOrMethod) StartTestItemRQ(com.epam.ta.reportportal.ws.model.StartTestItemRQ) Test(org.junit.jupiter.api.Test)

Example 2 with TestCaseId

use of com.epam.reportportal.annotations.TestCaseId in project agent-java-junit5 by reportportal.

the class ReportPortalExtension method buildStartConfigurationRq.

/**
 * Extension point to customize beforeXXX creation event/request
 *
 * @param method        JUnit's test method reference
 * @param parentContext JUnit's context of a parent item
 * @param context       JUnit's test context
 * @param itemType      a type of the item to build
 * @return Request to ReportPortal
 */
@Nonnull
protected StartTestItemRQ buildStartConfigurationRq(@Nonnull Method method, @Nonnull ExtensionContext parentContext, @Nonnull ExtensionContext context, @Nonnull ItemType itemType) {
    StartTestItemRQ rq = new StartTestItemRQ();
    rq.setStartTime(Calendar.getInstance().getTime());
    Optional<Class<?>> testClass = context.getTestClass();
    if (testClass.isPresent()) {
        rq.setName(createConfigurationName(testClass.get(), method));
        rq.setDescription(createConfigurationDescription(testClass.get(), method));
    } else {
        rq.setName(createConfigurationName(method.getDeclaringClass(), method));
        rq.setDescription(createConfigurationDescription(method.getDeclaringClass(), method));
    }
    String uniqueId = parentContext.getUniqueId() + "/[method:" + method.getName() + "()]";
    rq.setUniqueId(uniqueId);
    ofNullable(context.getTags()).ifPresent(it -> rq.setAttributes(it.stream().map(tag -> new ItemAttributesRQ(null, tag)).collect(Collectors.toSet())));
    rq.setType(itemType.name());
    rq.setRetry(false);
    String codeRef = method.getDeclaringClass().getCanonicalName() + "." + method.getName();
    rq.setCodeRef(codeRef);
    TestCaseIdEntry caseId = ofNullable(method.getAnnotation(TestCaseId.class)).map(TestCaseId::value).map(TestCaseIdEntry::new).orElseGet(() -> TestCaseIdUtils.getTestCaseId(codeRef, Collections.emptyList()));
    rq.setTestCaseId(ofNullable(caseId).map(TestCaseIdEntry::getId).orElse(null));
    return rq;
}
Also used : java.util(java.util) ListenerParameters(com.epam.reportportal.listeners.ListenerParameters) Launch(com.epam.reportportal.service.Launch) com.epam.ta.reportportal.ws.model(com.epam.ta.reportportal.ws.model) Maybe(io.reactivex.Maybe) LoggerFactory(org.slf4j.LoggerFactory) ReportPortal(com.epam.reportportal.service.ReportPortal) AttributeParser(com.epam.reportportal.utils.AttributeParser) TestCaseId(com.epam.reportportal.annotations.TestCaseId) TestItemTree(com.epam.reportportal.service.tree.TestItemTree) StringUtils(org.apache.commons.lang3.StringUtils) TestCaseIdEntry(com.epam.reportportal.service.item.TestCaseIdEntry) SystemAttributesFetcher.collectSystemAttributes(com.epam.reportportal.junit5.SystemAttributesFetcher.collectSystemAttributes) Attributes(com.epam.reportportal.annotations.attribute.Attributes) TestCaseIdUtils(com.epam.reportportal.utils.TestCaseIdUtils) Nonnull(javax.annotation.Nonnull) Method(java.lang.reflect.Method) Nullable(javax.annotation.Nullable) ParameterUtils(com.epam.reportportal.utils.ParameterUtils) TestItemTree.createTestItemLeaf(com.epam.reportportal.service.tree.TestItemTree.createTestItemLeaf) Logger(org.slf4j.Logger) Optional.ofNullable(java.util.Optional.ofNullable) TestAbortedException(org.opentest4j.TestAbortedException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ExceptionUtils.getStackTrace(org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace) ItemStatus(com.epam.reportportal.listeners.ItemStatus) SaveLogRQ(com.epam.ta.reportportal.ws.model.log.SaveLogRQ) ParameterKey(com.epam.reportportal.annotations.ParameterKey) Collectors(java.util.stream.Collectors) ItemAttributesRQ(com.epam.ta.reportportal.ws.model.attribute.ItemAttributesRQ) StartLaunchRQ(com.epam.ta.reportportal.ws.model.launch.StartLaunchRQ) org.junit.jupiter.api(org.junit.jupiter.api) ItemType(com.epam.reportportal.junit5.ItemType) org.junit.jupiter.api.extension(org.junit.jupiter.api.extension) ItemTreeUtils.createItemTreeKey(com.epam.reportportal.junit5.utils.ItemTreeUtils.createItemTreeKey) AnnotatedElement(java.lang.reflect.AnnotatedElement) TestCaseId(com.epam.reportportal.annotations.TestCaseId) ItemAttributesRQ(com.epam.ta.reportportal.ws.model.attribute.ItemAttributesRQ) TestCaseIdEntry(com.epam.reportportal.service.item.TestCaseIdEntry) Nonnull(javax.annotation.Nonnull)

Example 3 with TestCaseId

use of com.epam.reportportal.annotations.TestCaseId in project agent-java-junit5 by reportportal.

the class ReportPortalExtension method getTestCaseId.

/**
 * Calculates a test case ID based on code reference and parameters
 *
 * @param method    a test method reference
 * @param codeRef   a code reference which will be used for the calculation
 * @param arguments a list of test arguments
 * @param instance  current test instance
 * @return a test case ID
 */
protected TestCaseIdEntry getTestCaseId(@Nonnull final Method method, @Nonnull final String codeRef, @Nonnull final List<Object> arguments, @Nullable Object instance) {
    TestCaseId caseId = method.getAnnotation(TestCaseId.class);
    TestCaseIdEntry id = TestCaseIdUtils.getTestCaseId(caseId, method, codeRef, arguments, instance);
    if (id == null) {
        return null;
    }
    return id.getId().endsWith("[]") ? new TestCaseIdEntry(id.getId().substring(0, id.getId().length() - 2)) : id;
}
Also used : TestCaseId(com.epam.reportportal.annotations.TestCaseId) TestCaseIdEntry(com.epam.reportportal.service.item.TestCaseIdEntry)

Aggregations

TestCaseId (com.epam.reportportal.annotations.TestCaseId)3 ParameterKey (com.epam.reportportal.annotations.ParameterKey)2 ItemStatus (com.epam.reportportal.listeners.ItemStatus)2 Launch (com.epam.reportportal.service.Launch)2 TestCaseIdEntry (com.epam.reportportal.service.item.TestCaseIdEntry)2 Method (java.lang.reflect.Method)2 java.util (java.util)2 TestCaseIdKey (com.epam.reportportal.annotations.TestCaseIdKey)1 UniqueID (com.epam.reportportal.annotations.UniqueID)1 Attributes (com.epam.reportportal.annotations.attribute.Attributes)1 ItemType (com.epam.reportportal.junit5.ItemType)1 SystemAttributesFetcher.collectSystemAttributes (com.epam.reportportal.junit5.SystemAttributesFetcher.collectSystemAttributes)1 ItemTreeUtils.createItemTreeKey (com.epam.reportportal.junit5.utils.ItemTreeUtils.createItemTreeKey)1 ListenerParameters (com.epam.reportportal.listeners.ListenerParameters)1 ReportPortal (com.epam.reportportal.service.ReportPortal)1 TestItemTree (com.epam.reportportal.service.tree.TestItemTree)1 TestItemTree.createTestItemLeaf (com.epam.reportportal.service.tree.TestItemTree.createTestItemLeaf)1 Constants (com.epam.reportportal.testng.Constants)1 AttributeParser (com.epam.reportportal.utils.AttributeParser)1 MemoizingSupplier (com.epam.reportportal.utils.MemoizingSupplier)1