org.testng.ITestResult Java Examples

The following examples show how to use org.testng.ITestResult. 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: TestNGTestResultProcessorAdapter.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 8 votes vote down vote up
private void onTestFinished(ITestResult iTestResult, TestResult.ResultType resultType) {
    Object testId;
    TestStartEvent startEvent = null;
    synchronized (lock) {
        testId = tests.remove(iTestResult);
        if (testId == null) {
            // This can happen when a method fails which this method depends on 
            testId = idGenerator.generateId();
            Object parentId = testMethodToSuiteMapping.get(iTestResult.getMethod());
            startEvent = new TestStartEvent(iTestResult.getStartMillis(), parentId);
        }
    }
    if (startEvent != null) {
        // Synthesize a start event
        resultProcessor.started(new DefaultTestMethodDescriptor(testId, iTestResult.getTestClass().getName(), iTestResult.getName()), startEvent);
    }
    if (resultType == TestResult.ResultType.FAILURE) {
        resultProcessor.failure(testId, iTestResult.getThrowable());
    }
    resultProcessor.completed(testId, new TestCompleteEvent(iTestResult.getEndMillis(), resultType));
}
 
Example #2
Source File: Retry.java    From AppiumTestDistribution with GNU General Public License v3.0 7 votes vote down vote up
@Override
public boolean retry(ITestResult iTestResult) {
    int maxRetryCount;
    RetryCount annotation = iTestResult.getMethod().getConstructorOrMethod().getMethod()
            .getAnnotation(RetryCount.class);
    if (annotation != null) {
        maxRetryCount = annotation.maxRetryCount();
    } else {
        try {
            maxRetryCount = MAX_RETRY_COUNT.getInt();
        } catch (Exception e) {
            maxRetryCount = 0;
        }
    }
    return !iTestResult.isSuccess() && COUNTER.incrementAndGet() < maxRetryCount;
}
 
Example #3
Source File: ReportListener.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
private void logUrl(ITestResult tr) {
    TestContext testContext;
    Object[] parameters = tr.getParameters();
    if (parameters == null || parameters.length == 0) {
        return;
    }
    try {
        testContext = (TestContext) parameters[0];
    } catch (ClassCastException e) {
        return;
    }
    Iterable<Searchable> searchables = Iterables.filter(testContext.getResources().values(), Searchable.class);
    List<Searchable> listOfSearchables = StreamSupport.stream(searchables.spliterator(), false).collect(Collectors.toList());
    if (listOfSearchables.size() == 0) {
        return;
    }
    SearchUrl searchUrl = new KibanaSearchUrl();
    tr.getTestContext().setAttribute(tr.getName() + SEARCH_URL,
            searchUrl.getSearchUrl(listOfSearchables, new Date(tr.getStartMillis()), new Date(tr.getEndMillis())));
}
 
Example #4
Source File: QuantumReportiumListener.java    From Quantum with MIT License 6 votes vote down vote up
@Override
public void onTestSkipped(ITestResult result) {
	ReportiumClient client = getReportClient();
	if (null != client) {
		// By default all the skipped tests will be failed, if you want
		if (ConfigurationManager.getBundle().getString("skippedTests", "fail").equalsIgnoreCase("pass")) {
			client.testStop(TestResultFactory.createSuccess());
		} else {
			String failureMessage = result.getThrowable().getMessage();
			failureMessage = (failureMessage.isEmpty() || failureMessage == null) ? "This test was skipped"
					: result.getThrowable().getMessage();
			client.testStop(TestResultFactory.createFailure(failureMessage, result.getThrowable()));
		}
		logTestEnd(result);
	}
}
 
Example #5
Source File: AbstractTestListener.java    From carina with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #6
Source File: WebSetupManager.java    From functional-tests-core with Apache License 2.0 6 votes vote down vote up
/**
 * Init only once per configuration.
 *
 * @return Instance of MobileSetupManager.
 */
public static WebSetupManager init() {
    WebSetupManager webSetupManager;
    if (WebSetupManager.initSettings == null) {
        WebSetupManager.initSettings = new HashMap<>();
    }

    if (!WebSetupManager.initSettings.containsKey(Settings.getAppConfig())) {
        webSetupManager = new WebSetupManager();
        webSetupManager.context.settings = new WebSettings();
        webSetupManager.driver = new ChromeDriver(new ChromeDriverOptions().loadChromeDriverOptions(webSetupManager.context.settings));
        webSetupManager.context.driver = webSetupManager.driver;
        webSetupManager.context.log = new Log();
        webSetupManager.context.locators = new Locators();
        webSetupManager.context.lastTestResult = ITestResult.SUCCESS;
        webSetupManager.context.find = new Find(webSetupManager.driver);
        WebSetupManager.initSettings.put(getAppConfig(), webSetupManager);
    } else {
        webSetupManager = WebSetupManager.initSettings.get(getAppConfig());
    }

    return webSetupManager;
}
 
Example #7
Source File: TestNG_ConsoleRunner.java    From Selenium-Framework-Design-in-Data-Driven-Testing with MIT License 6 votes vote down vote up
/**
 * getTestDescription method
 *
 * @param tr
 * @return String
 */
public String getTestDescription(ITestResult tr) {
    String message = "";

    try {
        if ( tr.getParameters().length > 0 ) {
            message = ": " + tr.getParameters()[1].toString();
        }
    }

    catch(Exception e) {
        // do nothing...
    }

    return message;
}
 
Example #8
Source File: TestNGRemoteListenerTest.java    From video-recorder-java with MIT License 5 votes vote down vote up
@Test
@Video
public void shouldBeCustomFolderForVideo() {
System.setProperty("video.folder", System.getProperty("user.dir") + "/custom_folder");
  ITestResult result = prepareMock(testMethod);
  RemoteVideoListener listener = new RemoteVideoListener();
  listener.onTestStart(result);
  listener.onTestFailure(result);
  File file = MonteRecorder.getLastRecording();
  assertThat(file.getParentFile().getName(), equalTo("custom_folder"));
}
 
Example #9
Source File: JUnitXMLReporter.java    From olat with Apache License 2.0 5 votes vote down vote up
private static int getFailures(List<ITestResult> results) {
    int retval = 0;
    for (ITestResult result : results) {
        if (result != null && result.getStatus() == ITestResult.FAILURE)
            retval++;
    }
    return retval;
}
 
Example #10
Source File: AllureTestListener.java    From allure1 with Apache License 2.0 5 votes vote down vote up
@Override
public void onConfigurationFailure(ITestResult iTestResult) {
    if (isSuppressConfigEvent(iTestResult)) {
        return;
    }
    String suiteUid = getSuiteUid(iTestResult.getTestContext());
    if (isAfterSuiteConfigMethod(iTestResult)) {
        String suiteTitle = getCurrentSuiteTitle(iTestResult.getTestContext());
        getLifecycle().fire(new TestSuiteStartedEvent(suiteUid, suiteTitle).withTitle(suiteTitle));
    }
    Throwable throwable = iTestResult.getThrowable();
    createConfigEvent(iTestResult);
    getLifecycle().fire(new TestCaseFailureEvent().withThrowable(throwable));
    fireFinishTest();
    if (isAfterSuiteConfigMethod(iTestResult)) {
        getLifecycle().fire(new TestSuiteFinishedEvent(suiteUid));
    }
}
 
Example #11
Source File: VerboseReporter.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private ITestNGMethod[] resultsToMethods(List<ITestResult> results) {
    ITestNGMethod[] result = new ITestNGMethod[results.size()];
    int i = 0;
    for (ITestResult tr : results) {
        result[i++] = tr.getMethod();
    }
    return result;
}
 
Example #12
Source File: CarinaListener.java    From carina with Apache License 2.0 5 votes vote down vote up
@Override
public void onTestSkipped(ITestResult result) {
    LOGGER.debug("CarinaListener->onTestSkipped");
    String errorMessage = getFailureReason(result);
    takeScreenshot(result, "TEST FAILED - " + errorMessage);
    super.onTestSkipped(result);
    onTestFinish(result);  
}
 
Example #13
Source File: TagManagerTest.java    From carina-demo with Apache License 2.0 5 votes vote down vote up
@Test
@TestPriority(Priority.P2)
@TestTag(name = TAG_NAME2, value = TAG_VALUE2)
@TestTag(name = TAG_NAME, value = TAG_VALUE)
@TestTag(name = FORBIDDEN_KEY_PRIORITY, value = "P0")
public void testZafiraGetTagsMethod() {
    ITestResult result = Reporter.getCurrentTestResult();
    Map<String, String> tags = TagManager.getTags(result);
    String priority = PriorityManager.getPriority(result);
    Assert.assertEquals(priority, "P2");
    Assert.assertFalse(tags.containsKey(FORBIDDEN_KEY_PRIORITY));
    Assert.assertTrue(tags.containsKey(TAG_NAME));
    Assert.assertEquals(tags.get(TAG_NAME), TAG_VALUE);
    Assert.assertTrue(tags.containsKey(TAG_NAME2));
    Assert.assertEquals(tags.get(TAG_NAME2), TAG_VALUE2);
    Assert.assertEquals(tags.size(), 2);
    Set<TagType> tagsTypes = getTestTags(result);
    Assert.assertEquals(tagsTypes.size(), 3);
    for (TagType entry : tagsTypes) {
        if (entry.getName().equals(SpecialKeywords.TEST_PRIORITY_KEY)) {
            Assert.assertEquals(entry.getValue(), "P2");
        }
    }

    tagsTypes.stream().forEachOrdered((entry) -> {
        Object currentKey = entry.getName();
        Object currentValue = entry.getValue();
        LOGGER.info(currentKey + "=" + currentValue);
    });
}
 
Example #14
Source File: JUnitXMLReporter.java    From olat with Apache License 2.0 5 votes vote down vote up
private static int getErrors(List<ITestResult> results) {
    int retval = 0;
    for (ITestResult result : results) {
        if (result != null && result.getStatus() != ITestResult.SUCCESS && result.getStatus() != ITestResult.SUCCESS_PERCENTAGE_FAILURE
                && result.getStatus() != ITestResult.FAILURE)
            retval++;
    }
    return retval;
}
 
Example #15
Source File: TestHarness.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@AfterMethod
public void printError(ITestResult result) {
    if (result.getStatus() == ITestResult.FAILURE) {
        String clsName = result.getTestClass().getName();
        clsName = clsName.substring(clsName.lastIndexOf(".") + 1);
        System.out.println("Test " + clsName + "." +
                           result.getName() + " FAILED");
    }
}
 
Example #16
Source File: AtsTestngListener.java    From ats-framework with Apache License 2.0 5 votes vote down vote up
private void endTestcaseWithSkipStatus( ITestResult testResult, ITestContext context ) {

        //Check if the test was successfully started, if not - make it started and then end it with failure
        String testName = testResult.getMethod().toString();
        if (!testName.equals(currentTestcaseName)) {
            startTestcase(testResult);
        }

        sendTestEndEventToAgents();

        if (configurationError(context)) {
            // test is skipped due to configuration error
            logger.info(MSG__TEST_SKIPPED_CONFIGURATION, testResult.getThrowable());
        } else if (dependencyError(testResult, context)) {
            // test is skipped due to dependency error
            logger.info(MSG__TEST_SKIPPED_DEPENDENCY, testResult.getThrowable());
        } else {
            // we do not know the exact problem
            logger.fatal(MSG__TEST_SKIPPED_UNRECOGNIZED_REASON, testResult.getThrowable());
        }

        currentTestcaseName = null;
        lastTestcaseResult = TestCaseResult.SKIPPED.toInt();
        // end test case
        logger.endTestcase(TestCaseResult.SKIPPED);

    }
 
Example #17
Source File: LoggingTestCase.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@AfterMethod
public void after(ITestResult result) {
    if (!result.isSuccess()) {
        List<Object> list = new ArrayList<>();
        Collections.addAll(list, result.getParameters());
        list.add(context.toString());
        result.setParameters(list.toArray(new Object[list.size()]));
    }
}
 
Example #18
Source File: ScreenshotArtifact.java    From Selenium-Foundation with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public byte[] getArtifact(final ITestResult result) {
    // ensure current test result is set
    Reporter.setCurrentTestResult(result);
    Optional<WebDriver> optDriver = DriverManager.nabDriver(result.getInstance());
    return ScreenshotUtils.getArtifact(optDriver, result.getThrowable(), LOGGER);
}
 
Example #19
Source File: LoggingTestCase.java    From streamsupport with GNU General Public License v2.0 5 votes vote down vote up
@AfterMethod
public void after(ITestResult result) {
    if (!result.isSuccess()) {
        List<Object> list = new ArrayList<>();
        Collections.addAll(list, result.getParameters());
        list.add(context.toString());
        result.setParameters(list.toArray(new Object[list.size()]));
    }
}
 
Example #20
Source File: FDSeparateCompilationTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@AfterMethod
public void printCaseError(ITestResult result) {
    if (result.getStatus() == ITestResult.FAILURE) {
        Hierarchy hs = (Hierarchy)result.getParameters()[0];
        System.out.println("Separate compilation case " + hs);
        printCaseDetails(hs);
    }
}
 
Example #21
Source File: TestNG_ConsoleRunner.java    From Selenium-Framework-Design-in-Data-Driven-Testing with MIT License 5 votes vote down vote up
/**
 * onTestFailure method
 *
 * @param tr
 */
@Override
public void onTestFailure(ITestResult tr) {
    if ( !getTestMessage(tr).equals("") ) {
        log(getTestMessage(tr) + "\n");
    }

    log("    ***Result = FAILED\n");
    log(tr.getEndMillis(),"END  -> " + tr.getInstanceName() + "." + tr.getName());
    log("\n---\n");

    super.onTestFailure(tr);
}
 
Example #22
Source File: LoggingTestCase.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@AfterMethod
public void after(ITestResult result) {
    if (!result.isSuccess()) {
        List<Object> list = new ArrayList<>();
        Collections.addAll(list, result.getParameters());
        list.add(context.toString());
        result.setParameters(list.toArray(new Object[list.size()]));
    }
}
 
Example #23
Source File: ReactiveStreamsArquillianTck.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Override
public void onTestFailure(ITestResult result) {
    printResult(result, "FAILED");
    if (result.getThrowable() != null) {
        result.getThrowable().printStackTrace(System.out);
        lastFailure.set(result.getThrowable());
    }
    failures.add(result);
    failed.incrementAndGet();
}
 
Example #24
Source File: MethodOwnerTest.java    From carina with Apache License 2.0 5 votes vote down vote up
@Test
@MethodOwner(owner = DEFAULT_OWNER)
@MethodOwner(owner = ANDROID_OWNER, platform = SpecialKeywords.ANDROID)
@MethodOwner(owner = IOS_OWNER, platform = SpecialKeywords.IOS)
public void testDefaultMethodOwner() {
    ITestResult result = Reporter.getCurrentTestResult();
    String ownerName = Ownership.getMethodOwner(result);
    Assert.assertEquals(ownerName, DEFAULT_OWNER);
}
 
Example #25
Source File: HTMLReporter.java    From tomee with Apache License 2.0 5 votes vote down vote up
private List<ITestResult> doWrap(final List<ITestResult> raw) {
    final List<ITestResult> wrapped = new ArrayList<>(raw.size());
    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    for (final ITestResult result : raw) {
        wrapped.add(ITestResult.class.cast(
            Proxy.newProxyInstance(loader, API,
                new InvocationHandler() {
                    @Override
                    public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
                        if (method.getName().equals("getParameters")) {
                            return new Object[method.getParameterTypes().length];
                        }
                        return method.invoke(result, args);
                    }
                })
        ));
    }
    return wrapped;
}
 
Example #26
Source File: CacheValidationListener.java    From caffeine with Apache License 2.0 5 votes vote down vote up
/** Free memory by clearing unused resources after test execution. */
private void cleanUp(ITestResult testResult) {
  boolean briefParams = !detailedParams.get();

  if (testResult.isSuccess() && briefParams) {
    testResult.setParameters(EMPTY_PARAMS);
    testResult.setThrowable(null);
  }

  Object[] params = testResult.getParameters();
  for (int i = 0; i < params.length; i++) {
    Object param = params[i];
    if ((param instanceof AsyncCache<?, ?>) || (param instanceof Cache<?, ?>)
        || (param instanceof Map<?, ?>) || (param instanceof Eviction<?, ?>)
        || (param instanceof Expiration<?, ?>) || (param instanceof VarExpiration<?, ?>)
        || ((param instanceof CacheContext) && briefParams)) {
      params[i] = simpleNames.get(param.getClass(), key -> ((Class<?>) key).getSimpleName());
    } else if (param instanceof CacheContext) {
      params[i] = simpleNames.get(param.toString(), Object::toString);
    } else {
      params[i] = Objects.toString(param);
    }
  }

  /*
  // Enable in TestNG 7.0
  if ((testResult.getName() != null) && briefParams) {
    testResult.setTestName(simpleNames.get(testResult.getName(), Object::toString));
  }
  */

  CacheSpec.interner.get().clear();
}
 
Example #27
Source File: TestNamingListener.java    From carina with Apache License 2.0 5 votes vote down vote up
/**
 * get Test Method name
 * 
 * @param result ITestResult
 * @return String method name
 */
public static String getMethodName(ITestResult result) {
    // adjust testName using pattern
    ITestNGMethod m = result.getMethod();
    String name = Configuration.get(Configuration.Parameter.TEST_NAMING_PATTERN);
    name = name.replace(SpecialKeywords.METHOD_NAME, m.getMethodName());
    name = name.replace(SpecialKeywords.METHOD_PRIORITY, String.valueOf(m.getPriority()));
    name = name.replace(SpecialKeywords.METHOD_THREAD_POOL_SIZE, String.valueOf(m.getThreadPoolSize()));

    if (m.getDescription() != null) {
        name = name.replace(SpecialKeywords.METHOD_DESCRIPTION, m.getDescription());
    } else {
        name = name.replace(SpecialKeywords.METHOD_DESCRIPTION, "");
    }

    return name;
}
 
Example #28
Source File: BaseWebDriverTest.java    From demo-java with MIT License 5 votes vote down vote up
@AfterMethod
public void tearDown(ITestResult result) {
    try {
        if (runType.equals(RunType.SAUCE)) {
            ((JavascriptExecutor) webDriver.get()).executeScript("sauce:job-result=" + (result.isSuccess() ? "passed" : "failed"));
        }
    }
    finally {
        webDriver.get().quit();
    }
}
 
Example #29
Source File: QuickPerfTestNGListener.java    From quickperf with Apache License 2.0 5 votes vote down vote up
@Override
public void run(IHookCallBack hookCallBack, ITestResult testResult) {

    TestExecutionContext testExecutionContext = buildTestExecutionContext(testResult);

    if(testExecutionContext.isQuickPerfDisabled()) {
        hookCallBack.runTestMethod(testResult);
        return;
    }

    if(SystemProperties.TEST_CODE_EXECUTING_IN_NEW_JVM.evaluate()) {
        executeTestMethodInNewJvmAndRecordPerformance(testResult, testExecutionContext);
        return;
    }

    JvmOrTestIssue jvmOrTestIssue =
            executeTestMethodAndRecordPerformance(hookCallBack, testResult, testExecutionContext);

    SetOfAnnotationConfigs testAnnotationConfigs = quickPerfConfigs.getTestAnnotationConfigs();
    Collection<PerfIssuesToFormat> groupOfPerfIssuesToFormat = perfIssuesEvaluator.evaluatePerfIssuesIfNoJvmIssue(testAnnotationConfigs
                                                                                                                , testExecutionContext
                                                                                                                , jvmOrTestIssue);

    testExecutionContext.cleanResources();

    try {
        quickPerfReporter.report(jvmOrTestIssue, groupOfPerfIssuesToFormat, testExecutionContext);
    } catch (Throwable throwable) {
        testResult.setThrowable(throwable);
        testResult.setStatus(ITestResult.FAILURE);
    }

}
 
Example #30
Source File: TagManagerTest.java    From carina with Apache License 2.0 5 votes vote down vote up
@Test
@TestPriority(Priority.P2)
@TestTag(name = TAG_NAME2, value = TAG_VALUE2)
@TestTag(name = TAG_NAME, value = TAG_VALUE)
@TestTag(name = FORBIDDEN_KEY_PRIORITY, value = "P0")
public void testForbiddenTags() {
    ITestResult result = Reporter.getCurrentTestResult();
    Map<String, String> tags = TagManager.getTags(result);
    Assert.assertFalse(tags.containsKey(FORBIDDEN_KEY_PRIORITY));
    Assert.assertTrue(tags.containsKey(TAG_NAME));
    Assert.assertEquals(tags.get(TAG_NAME), TAG_VALUE);
    Assert.assertTrue(tags.containsKey(TAG_NAME2));
    Assert.assertEquals(tags.get(TAG_NAME2), TAG_VALUE2);
    Assert.assertEquals(tags.size(), 2);
}