Java Code Examples for org.testng.ITestResult#getTestContext()

The following examples show how to use org.testng.ITestResult#getTestContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AllureTestListener.java    From allure1 with Apache License 2.0 6 votes vote down vote up
/**
 * Suppress duplicated configuration method events
 */
@SuppressWarnings("unchecked")
private synchronized boolean isSuppressConfigEvent(ITestResult iTestResult) {
    Set<String> methodNames;
    ITestContext context = iTestResult.getTestContext();
    String configType = getConfigMethodType(iTestResult).getName();
    if (context.getAttribute(configType) == null) {
        methodNames = new HashSet<>();
        methodNames.add(iTestResult.getName());
        context.setAttribute(configType, methodNames);
        return false;
    } else {
        methodNames = (Set<String>) context.getAttribute(configType);
        if (!methodNames.contains(iTestResult.getName())) {
            methodNames.add(iTestResult.getName());
            return false;
        }
    }
    return true;
}
 
Example 2
Source File: AllureTestNg.java    From allure-java with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeInvocation(final IInvokedMethod method, final ITestResult testResult) {
    final ITestNGMethod testMethod = method.getTestMethod();
    final ITestContext context = testResult.getTestContext();
    if (isSupportedConfigurationFixture(testMethod)) {
        ifSuiteFixtureStarted(context.getSuite(), testMethod);
        ifTestFixtureStarted(context, testMethod);
        ifClassFixtureStarted(testMethod);
        ifMethodFixtureStarted(testMethod);
    }
}
 
Example 3
Source File: CustomTestNgListener.java    From heat with Apache License 2.0 5 votes vote down vote up
/**
 * This method is useful to print the output console log in case of test failed.
 * We are assuming that we put in the context an attribute whose name is the complete test case ID (example: TEST_SUITE.001) and whose value is
 * 'PASSED' or 'SKIPPED' or 'FAILED'.
 * @param tr test case result - testNG handling
 */
@Override
public void onTestFailure(ITestResult tr) {
    if (tr.getParameters().length > 0) {
        Map<String, String> paramMap = (HashMap<String, String>) tr.getParameters()[0];
        ITestContext testContext = tr.getTestContext();
        //testCaseCompleteID - Example: TEST_SUITE.001
        String testCaseCompleteID = testContext.getName() + TestBaseRunner.TESTCASE_ID_SEPARATOR + testContext.getAttribute(TestBaseRunner.ATTR_TESTCASE_ID);
        logger.error("[{}][{}][{}] -- FAILED", testCaseCompleteID,
                    testContext.getAttribute(TestBaseRunner.SUITE_DESCRIPTION_CTX_ATTR).toString(),
                    testContext.getAttribute(TestBaseRunner.TC_DESCRIPTION_CTX_ATTR).toString());

        if (testContext.getAttribute(FAILED_TEST_CASES) == null) {
            failedTc = new ArrayList();
        } else {
            failedTc = (List<ITestResult>) testContext.getAttribute(FAILED_TEST_CASES);
        }
        failedTc.add(tr);
        testContext.setAttribute(FAILED_TEST_CASES, failedTc);

    } else {
        super.onTestFailure(tr);
    }
}
 
Example 4
Source File: CustomTestNgListener.java    From heat with Apache License 2.0 5 votes vote down vote up
/**
 * This method is useful to print the output console log in case of test success or test skipped.
 * We are assuming that we put in the context an attribute whose name is the complete test case ID (example: TEST_SUITE.001) and whose value is
 * 'PASSED' or 'SKIPPED' or 'FAILED'.
 * @param tr test case result - testNG handling
 */
@Override
public void onTestSuccess(ITestResult tr) {
    if (tr.getParameters().length > 0) {
        Map<String, String> paramMap = (HashMap<String, String>) tr.getParameters()[0];
        ITestContext testContext = tr.getTestContext();
        //testCaseCompleteID - Example: TEST_SUITE.001
        String testCaseCompleteID = testContext.getName() + TestBaseRunner.TESTCASE_ID_SEPARATOR + testContext.getAttribute(TestBaseRunner.ATTR_TESTCASE_ID);
        if (testContext.getAttributeNames().contains(testCaseCompleteID)
                && TestBaseRunner.STATUS_SKIPPED.equals(testContext.getAttribute(testCaseCompleteID))) {
            logger.info("[{}][{}][{}] -- SKIPPED", testCaseCompleteID,
                    testContext.getAttribute(TestBaseRunner.SUITE_DESCRIPTION_CTX_ATTR).toString(),
                    testContext.getAttribute(TestBaseRunner.TC_DESCRIPTION_CTX_ATTR).toString());

            if (testContext.getAttribute(SKIPPED_TEST_CASES) == null) {
                skippedTc = new ArrayList();
            } else {
                skippedTc = (List<ITestResult>) testContext.getAttribute(SKIPPED_TEST_CASES);
            }
            skippedTc.add(tr);
            testContext.setAttribute(SKIPPED_TEST_CASES, skippedTc);

        } else {
            logger.info("[{}][{}][{}] -- PASSED", testCaseCompleteID,
                    testContext.getAttribute(TestBaseRunner.SUITE_DESCRIPTION_CTX_ATTR).toString(),
                    testContext.getAttribute(TestBaseRunner.TC_DESCRIPTION_CTX_ATTR).toString());

            if (testContext.getAttribute(PASSED_TEST_CASES) == null) {
                passedTc = new ArrayList();
            } else {
                passedTc = (List<ITestResult>) testContext.getAttribute(PASSED_TEST_CASES);
            }
            passedTc.add(tr);
            testContext.setAttribute(PASSED_TEST_CASES, passedTc);

        }
    } else {
        super.onTestSuccess(tr);
    }
}
 
Example 5
Source File: AbstractTestListener.java    From carina with Apache License 2.0 5 votes vote down vote up
private void removeRetriedTests(ITestResult result) {
    ITestContext context = result.getTestContext();
    long passedTestId = getMethodId(result);
    LOGGER.debug("passedTest: " + passedTestId);

    // Removed failed retries for passed tests
    for (Iterator<ITestResult> iterator = context.getFailedTests()
            .getAllResults().iterator(); iterator.hasNext();) {
        ITestResult testResult = iterator.next();
        if (getMethodId(testResult) == passedTestId) {
            LOGGER.debug("Removed test retry from context: " + testResult.getName());
            iterator.remove();
        }
    }
}
 
Example 6
Source File: AbstractTestListener.java    From carina with Apache License 2.0 4 votes vote down vote up
@Override
public void onTestStart(ITestResult result) {
    LOGGER.debug("AbstractTestListener->onTestStart");
    VideoAnalyzer.disableVideoUpload();
    IRetryAnalyzer curRetryAnalyzer = getRetryAnalyzer(result);
    if (curRetryAnalyzer == null) {
        // Declare carina custom RetryAnalyzer annotation for each new test method. Handle use-case for data providers which has single method!
        // result.getMethod().setRetryAnalyzer(new RetryAnalyzer());
        result.getMethod().setRetryAnalyzerClass(RetryAnalyzer.class);
    } else {
        if (!(curRetryAnalyzer instanceof RetryAnalyzer)) {
            LOGGER.warn("Custom RetryAnalyzer is used: " + curRetryAnalyzer.getClass().getName());                
        }
        
    }
    
    generateParameters(result);

    if (!result.getTestContext().getCurrentXmlTest().getAllParameters()
            .containsKey(SpecialKeywords.EXCEL_DS_CUSTOM_PROVIDER) &&
            result.getParameters().length > 0) // set parameters from XLS only if test contains any parameter at
                                               // all)
    {
        if (result.getTestContext().getCurrentXmlTest().getAllParameters()
                .containsKey(SpecialKeywords.EXCEL_DS_ARGS)) {
            DSBean dsBean = new DSBean(result.getTestContext());
            int index = 0;
            for (String arg : dsBean.getArgs()) {
                dsBean.getTestParams().put(arg, (String) result.getParameters()[index++]);
            }
            result.getTestContext().getCurrentXmlTest().setParameters(dsBean.getTestParams());

        }
    }
    // obligatory reset any registered canonical name because for ALREADY_PASSED methods we can't do this in
    // onTestSkipped method
    // TestNamingUtil.releaseTestInfoByThread();

    startItem(result, Messager.TEST_STARTED);

}
 
Example 7
Source File: TestNamingListener.java    From carina with Apache License 2.0 4 votes vote down vote up
/**
 * Set full test name based on test class, method and other generic information. It is generated based by ITestResult object.
 * 
 * @param ITestResult result
 * @return String test name
 */     
@SuppressWarnings("unlikely-arg-type")
private static String setTestName(ITestResult result) {
    String name = "";

    if (result.getTestContext() == null) {
        throw new RuntimeException("Unable to set Test name without testContext!");
    }
    @SuppressWarnings("unchecked")
    Map<Object[], String> testnameMap = (Map<Object[], String>) result.getTestContext().getAttribute(SpecialKeywords.TEST_NAME_ARGS_MAP);

    if (testnameMap != null) {
        String testHash = String.valueOf(Arrays.hashCode(result.getParameters()));
        if (testnameMap.containsKey(testHash)) {
            name = testnameMap.get(testHash);
        }
    }

    if (name.isEmpty()) {
        name = result.getTestContext().getCurrentXmlTest().getName();
    }

    // TODO: find the bext way to calculate TUID/hash
    if (result.getTestContext().getCurrentXmlTest().getAllParameters().containsKey(SpecialKeywords.EXCEL_DS_CUSTOM_PROVIDER) ||
            result.getTestContext().getCurrentXmlTest().getAllParameters().containsKey(SpecialKeywords.DS_CUSTOM_PROVIDER)) {
        // AUTO-274 "Pass"ing status set on emailable report when a test step fails
        String methodUID = "";
        for (int i = 0; i < result.getParameters().length; i++) {
            if (result.getParameters()[i] != null) {
                if (result.getParameters()[i].toString().contains(SpecialKeywords.TUID + ":")) {
                    methodUID = result.getParameters()[i].toString().replace(SpecialKeywords.TUID + ":", "");
                    break; // first TUID: parameter is used
                }
            }
        }
        if (!methodUID.isEmpty()) {
            name = methodUID + " - " + name;
        }
    }

    name = name + " - " + getMethodName(result);
    LOGGER.debug("testName: " + name);
    
    // introduce invocation count calculation here as in multi threading mode TestNG doesn't provide valid
    // getInvocationCount() value
    int index = ((TestResult) result).getParameterIndex();
    if (index > 0) {
        // that's a dataprovider line index
        index++; //to make correlation between line and index number
        LOGGER.debug("test: " + name  + "; index: " + index);
        name = name + String.format(SpecialKeywords.DAPAPROVIDER_INDEX, String.format("%04d", index));
    }
    
    ITestNGMethod[] methods = result.getTestContext().getAllTestMethods();
    if (methods.length > 0) {
        int invCount = methods[0].getInvocationCount();
        if (invCount > 1) {
            LOGGER.debug("Detected method '" + result.getMethod().getMethodName() + "' with non zero invocationCount: " + invCount);
            int countIndex = getCurrentInvocationCount(name);
            LOGGER.debug("test: " + name + "; InvCount index: " + countIndex);
            name = name + String.format(SpecialKeywords.INVOCATION_COUNTER, String.format("%04d", countIndex));
        }
    }
    
    testName.set(name);
    return testName.get();
}