Search in sources :

Example 11 with ITestResult

use of org.testng.ITestResult in project ats-framework by Axway.

the class AtsTestngTestListener method dependencyError.

private boolean dependencyError(ITestResult testResult) {
    String[] dependentMethods = testResult.getMethod().getMethodsDependedUpon();
    List<ITestResult> failedTests = getFailedTests();
    for (String dependentMethod : dependentMethods) {
        for (ITestResult failedTestResult : failedTests) {
            String failedMethodName = new StringBuilder().append(failedTestResult.getTestClass().getName()).append(".").append(failedTestResult.getName()).toString();
            if (failedMethodName.equals(dependentMethod)) {
                logger.error("Dependent method '" + dependentMethod + "' failed!", failedTestResult.getThrowable());
                return true;
            }
        }
    }
    return false;
}
Also used : ITestResult(org.testng.ITestResult)

Example 12 with ITestResult

use of org.testng.ITestResult in project sonar-java by SonarSource.

the class TestNGListenerTest method mockTestResult.

private ITestResult mockTestResult() {
    ITestResult testResult = mock(ITestResult.class);
    ITestClass testClass = mock(ITestClass.class);
    when(testResult.getTestClass()).thenReturn(testClass);
    ITestNGMethod testMethod = mock(ITestNGMethod.class);
    when(testResult.getMethod()).thenReturn(testMethod);
    when(testClass.getName()).thenReturn("class");
    when(testMethod.getMethodName()).thenReturn("method");
    return testResult;
}
Also used : ITestClass(org.testng.ITestClass) ITestResult(org.testng.ITestResult) ITestNGMethod(org.testng.ITestNGMethod)

Example 13 with ITestResult

use of org.testng.ITestResult in project carina by qaprosoft.

the class AbstractTestListener method removeIncorrectlyFailedTests.

/**
 * When the test is restarted this method cleans fail statistics in test context.
 */
private void removeIncorrectlyFailedTests(ITestContext context) {
    // List of test results which we will delete later
    List<ITestResult> testsToBeRemoved = new ArrayList<>();
    // collect all id's from passed test
    Set<Long> passedTestIds = new HashSet<>();
    for (ITestResult passedTest : context.getPassedTests().getAllResults()) {
        // adding passed test
        long passedTestId = getMethodId(passedTest);
        // LOGGER.debug("Adding passedTest info: " + passedTestId + "; " + passedTest.getName());
        passedTestIds.add(passedTestId);
    }
    // LOGGER.debug("---------------- ANALYZE FAILED RESULTS FOR DUPLICATES -----------------------");
    Set<Long> failedTestIds = new HashSet<>();
    for (ITestResult failedTest : context.getFailedTests().getAllResults()) {
        // id = class + method + dataprovider
        long failedTestId = getMethodId(failedTest);
        // or delete this failed test if there is at least one passed version
        if (failedTestIds.contains(failedTestId) || passedTestIds.contains(failedTestId)) {
            // LOGGER.debug("Test to be removed from context: " + failedTestId + "; " + failedTest.getName());
            testsToBeRemoved.add(failedTest);
        } else {
            // LOGGER.debug("Test to mark as failed: " + failedTestId + "; " + failedTest.getName());
            failedTestIds.add(failedTestId);
        }
    }
    // finally delete all tests that are marked for removal
    for (Iterator<ITestResult> iterator = context.getFailedTests().getAllResults().iterator(); iterator.hasNext(); ) {
        ITestResult testResult = iterator.next();
        if (testsToBeRemoved.contains(testResult)) {
            // LOGGER.debug("Removing test from context: " + testResult.getName());
            iterator.remove();
        }
    }
}
Also used : ITestResult(org.testng.ITestResult)

Example 14 with ITestResult

use of org.testng.ITestResult in project carina by qaprosoft.

the class AbstractTestListener method skipItem.

private String skipItem(ITestResult result, Messager messager) {
    String test = TestNamingUtil.getCanonicalTestName(result);
    String errorMessage = getFailureReason(result);
    if (errorMessage.isEmpty()) {
        // identify is it due to the dependent failure or exception in before suite/class/method
        String[] methods = result.getMethod().getMethodsDependedUpon();
        // find if any parent method failed/skipped
        boolean dependentMethod = false;
        String dependentMethodName = "";
        for (ITestResult failedTest : result.getTestContext().getFailedTests().getAllResults()) {
            for (int i = 0; i < methods.length; i++) {
                if (methods[i].contains(failedTest.getName())) {
                    dependentMethodName = failedTest.getName();
                    dependentMethod = true;
                    break;
                }
            }
        }
        for (ITestResult skippedTest : result.getTestContext().getSkippedTests().getAllResults()) {
            for (int i = 0; i < methods.length; i++) {
                if (methods[i].contains(skippedTest.getName())) {
                    dependentMethodName = skippedTest.getName();
                    dependentMethod = true;
                    break;
                }
            }
        }
        if (dependentMethod) {
            errorMessage = "Test skipped due to the dependency from: " + dependentMethodName;
        } else {
            // Try to find error details from last configuration failure in this thread
            TestResultItem resultItem = getConfigFailure();
            if (resultItem != null) {
                errorMessage = resultItem.getFailReason();
            }
        }
    }
    String deviceName = getDeviceName();
    messager.info(deviceName, test, DateUtils.now(), errorMessage);
    EmailReportItemCollector.push(createTestResult(result, TestResultType.SKIP, errorMessage, result.getMethod().getDescription()));
    result.getTestContext().removeAttribute(SpecialKeywords.TEST_FAILURE_MESSAGE);
    return errorMessage;
}
Also used : ITestResult(org.testng.ITestResult) TestResultItem(com.qaprosoft.carina.core.foundation.report.TestResultItem)

Example 15 with ITestResult

use of org.testng.ITestResult in project carina by qaprosoft.

the class AbstractTestListener method getFailureReason.

protected String getFailureReason(ITestResult result) {
    String errorMessage = "";
    String message = "";
    if (result.getThrowable() != null) {
        Throwable thr = result.getThrowable();
        errorMessage = getFullStackTrace(thr);
        message = thr.getMessage();
        result.getTestContext().setAttribute(SpecialKeywords.TEST_FAILURE_MESSAGE, message);
    }
    // handle in case of failed config (exclusion of expected skip)
    if (errorMessage.isEmpty()) {
        String methodName;
        Collection<ITestResult> results = result.getTestContext().getSkippedConfigurations().getAllResults();
        for (ITestResult resultItem : results) {
            methodName = resultItem.getMethod().getMethodName();
            if (methodName.equals(SpecialKeywords.BEFORE_TEST_METHOD)) {
                errorMessage = getFullStackTrace(resultItem.getThrowable());
            }
        }
    }
    return errorMessage;
}
Also used : ITestResult(org.testng.ITestResult)

Aggregations

ITestResult (org.testng.ITestResult)24 ArrayList (java.util.ArrayList)5 ITestNGMethod (org.testng.ITestNGMethod)5 Test (org.testng.annotations.Test)4 InvokedMethodNameListener (io.github.sskorol.listeners.InvokedMethodNameListener)3 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 List (java.util.List)2 EntryStream (one.util.streamex.EntryStream)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 ITestClass (org.testng.ITestClass)2 TestResultItem (com.qaprosoft.carina.core.foundation.report.TestResultItem)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 IInvokedMethod (org.testng.IInvokedMethod)1 IResultMap (org.testng.IResultMap)1 ITestContext (org.testng.ITestContext)1