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, Alexandru Popescu
/** * A simple ITestListener adapter that stores all the tests that were run. You can retrieve these * results with the following methods: getPassedTests() getFailedTests() getSkippedTests() * * <p>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 <a href='mailto:the_mindstorm@evolva.ro'>Alexandru Popescu</a> */
public class TestListenerAdapter implements IResultListener2 { private Collection<ITestNGMethod> m_allTestMethods = new ConcurrentLinkedQueue<>(); private Collection<ITestResult> m_passedTests = new ConcurrentLinkedQueue<>(); private Collection<ITestResult> m_failedTests = new ConcurrentLinkedQueue<>(); private Collection<ITestResult> m_skippedTests = new ConcurrentLinkedQueue<>(); private Collection<ITestResult> m_failedButWSPerTests = new ConcurrentLinkedQueue<>(); private Collection<ITestContext> m_testContexts = new ConcurrentLinkedQueue<>(); private Collection<ITestResult> m_failedConfs = new ConcurrentLinkedQueue<>(); private Collection<ITestResult> m_skippedConfs = new ConcurrentLinkedQueue<>(); private Collection<ITestResult> m_passedConfs = new ConcurrentLinkedQueue<>(); private Collection<ITestResult> m_timedOutTests = 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 onTestFailedWithTimeout(ITestResult tr) { m_allTestMethods.add(tr.getMethod()); m_timedOutTests.add(tr); onTestFailure(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[0]); } @Override public void onStart(ITestContext testContext) { m_testContexts.add(testContext); } @Override public void onFinish(ITestContext testContext) {}
Returns:Returns the failedButWithinSuccessPercentageTests.
/** @return Returns the failedButWithinSuccessPercentageTests. */
public List<ITestResult> getFailedButWithinSuccessPercentageTests() { return new ArrayList<>(m_failedButWSPerTests); }
Returns:Returns the failedTests.
/** @return Returns the failedTests. */
public List<ITestResult> getFailedTests() { return new ArrayList<>(m_failedTests); }
Returns:Returns the passedTests.
/** @return Returns the passedTests. */
public List<ITestResult> getPassedTests() { return new ArrayList<>(m_passedTests); }
Returns:Returns the skippedTests.
/** @return Returns the skippedTests. */
public List<ITestResult> getSkippedTests() { return new ArrayList<>(m_skippedTests); }
Returns:Returns the tests that failed due to a timeout
/** * @return Returns the tests that failed due to a timeout */
public Collection<ITestResult> getTimedoutTests() { return new ArrayList<>(m_timedOutTests); }
Params:
  • allTestMethods – The allTestMethods to set.
/** @param allTestMethods The allTestMethods to set. */
public void setAllTestMethods(List<ITestNGMethod> allTestMethods) { m_allTestMethods = allTestMethods; }
Params:
  • failedButWithinSuccessPercentageTests – The failedButWithinSuccessPercentageTests to set.
/** * @param failedButWithinSuccessPercentageTests The failedButWithinSuccessPercentageTests to set. */
public void setFailedButWithinSuccessPercentageTests( List<ITestResult> failedButWithinSuccessPercentageTests) { m_failedButWSPerTests = failedButWithinSuccessPercentageTests; }
Params:
  • failedTests – The failedTests to set.
/** @param failedTests The failedTests to set. */
public void setFailedTests(List<ITestResult> failedTests) { m_failedTests = failedTests; }
Params:
  • passedTests – The passedTests to set.
/** @param passedTests The passedTests to set. */
public void setPassedTests(List<ITestResult> passedTests) { m_passedTests = passedTests; }
Params:
  • skippedTests – The skippedTests to set.
/** @param skippedTests The skippedTests to set. */
public void setSkippedTests(List<ITestResult> skippedTests) { m_skippedTests = skippedTests; } @Override public void onTestStart(ITestResult result) {} public List<ITestContext> getTestContexts() { return new ArrayList<>(m_testContexts); } public List<ITestResult> getConfigurationFailures() { return new ArrayList<>(m_failedConfs); }
See Also:
/** @see org.testng.IConfigurationListener#onConfigurationFailure(org.testng.ITestResult) */
@Override public void onConfigurationFailure(ITestResult itr) { m_failedConfs.add(itr); } public List<ITestResult> getConfigurationSkips() { return new ArrayList<>(m_skippedConfs); } @Override public void beforeConfiguration(ITestResult tr) {}
See Also:
/** @see org.testng.IConfigurationListener#onConfigurationSkip(org.testng.ITestResult) */
@Override public void onConfigurationSkip(ITestResult itr) { m_skippedConfs.add(itr); }
See Also:
/** @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(); } }