All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testng.TestListenerAdapter Maven / Gradle / Ivy

There is a newer version: 7.10.1
Show newest version
package org.testng;

import org.testng.collections.Objects;
import org.testng.internal.IResultListener2;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;


/**
 * A simple ITestListener adapter that stores all the tests
 * that were run.  You can retrieve these results with the
 * following methods:
 * getPassedTests()
 * getFailedTests()
 * getSkippedTests()
 *
 * If you extend this class in order to override any of these
 * methods, remember to call their super equivalent if you want
 * this list of tests to be maintained.
 *
 * @author Cedric Beust, Aug 6, 2004
 * @author Alexandru Popescu
 */
public class TestListenerAdapter implements IResultListener2 {
  private Collection m_allTestMethods = new ConcurrentLinkedQueue<>();
  private Collection m_passedTests = new ConcurrentLinkedQueue<>();
  private Collection m_failedTests = new ConcurrentLinkedQueue<>();
  private Collection m_skippedTests = new ConcurrentLinkedQueue<>();
  private Collection m_failedButWSPerTests = new ConcurrentLinkedQueue<>();
  private Collection m_testContexts = new ConcurrentLinkedQueue<>();
  private Collection m_failedConfs = new ConcurrentLinkedQueue<>();
  private Collection m_skippedConfs = new ConcurrentLinkedQueue<>();
  private Collection m_passedConfs = new ConcurrentLinkedQueue<>();

  @Override
  public void onTestSuccess(ITestResult tr) {
    m_allTestMethods.add(tr.getMethod());
    m_passedTests.add(tr);
  }

  @Override
  public void onTestFailure(ITestResult tr) {
    m_allTestMethods.add(tr.getMethod());
    m_failedTests.add(tr);
  }

  @Override
  public void onTestSkipped(ITestResult tr) {
    m_allTestMethods.add(tr.getMethod());
    m_skippedTests.add(tr);
  }

  @Override
  public void onTestFailedButWithinSuccessPercentage(ITestResult tr) {
    m_allTestMethods.add(tr.getMethod());
    m_failedButWSPerTests.add(tr);
  }

  protected ITestNGMethod[] getAllTestMethods() {
    return m_allTestMethods.toArray(new ITestNGMethod[m_allTestMethods.size()]);
  }

  @Override
  public void onStart(ITestContext testContext) {
	  m_testContexts.add(testContext);
  }

  @Override
  public void onFinish(ITestContext testContext) {
  }

  /**
   * @return Returns the failedButWithinSuccessPercentageTests.
   */
  public List getFailedButWithinSuccessPercentageTests() {
    return new ArrayList<>(m_failedButWSPerTests);
  }
  /**
   * @return Returns the failedTests.
   */
  public List getFailedTests() {
    return new ArrayList<>(m_failedTests);
  }
  /**
   * @return Returns the passedTests.
   */
  public List getPassedTests() {
    return new ArrayList<>(m_passedTests);
  }
  /**
   * @return Returns the skippedTests.
   */
  public List getSkippedTests() {
    return new ArrayList<>(m_skippedTests);
  }

  private static void ppp(String s) {
    System.out.println("[TestListenerAdapter] " + s);
  }
  /**
   * @param allTestMethods The allTestMethods to set.
   */
  public void setAllTestMethods(List allTestMethods) {
    m_allTestMethods = allTestMethods;
  }
  /**
   * @param failedButWithinSuccessPercentageTests The failedButWithinSuccessPercentageTests to set.
   */
  public void setFailedButWithinSuccessPercentageTests(
      List failedButWithinSuccessPercentageTests) {
    m_failedButWSPerTests = failedButWithinSuccessPercentageTests;
  }
  /**
   * @param failedTests The failedTests to set.
   */
  public void setFailedTests(List failedTests) {
    m_failedTests = failedTests;
  }
  /**
   * @param passedTests The passedTests to set.
   */
  public void setPassedTests(List passedTests) {
    m_passedTests = passedTests;
  }
  /**
   * @param skippedTests The skippedTests to set.
   */
  public void setSkippedTests(List skippedTests) {
    m_skippedTests = skippedTests;
  }

  @Override
  public void onTestStart(ITestResult result) {
  }

  public List getTestContexts() {
    return new ArrayList<>(m_testContexts);
  }

  public List getConfigurationFailures() {
    return new ArrayList<>(m_failedConfs);
  }

  /**
   * @see org.testng.IConfigurationListener#onConfigurationFailure(org.testng.ITestResult)
   */
  @Override
  public void onConfigurationFailure(ITestResult itr) {
    m_failedConfs.add(itr);
  }

  public List getConfigurationSkips() {
    return new ArrayList<>(m_skippedConfs);
  }

  @Override
  public void beforeConfiguration(ITestResult tr) {
  }

  /**
   * @see org.testng.IConfigurationListener#onConfigurationSkip(org.testng.ITestResult)
   */
  @Override
  public void onConfigurationSkip(ITestResult itr) {
    m_skippedConfs.add(itr);
  }

  /**
   * @see org.testng.IConfigurationListener#onConfigurationSuccess(org.testng.ITestResult)
   */
  @Override
  public void onConfigurationSuccess(ITestResult itr) {
    m_passedConfs.add(itr);
  }

  @Override
  public String toString() {
    return Objects.toStringHelper(getClass())
        .add("passed", getPassedTests().size())
        .add("failed", getFailedTests().size())
        .add("skipped", getSkippedTests().size())
        .toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy