1 package test.multiplelisteners; 2 3 import org.testng.TestListenerAdapter; 4 import org.testng.TestNG; 5 import org.testng.annotations.Test; 6 import org.testng.collections.Lists; 7 import org.testng.xml.XmlClass; 8 import org.testng.xml.XmlSuite; 9 import org.testng.xml.XmlTest; 10 11 import java.util.Arrays; 12 import java.util.Collections; 13 import java.util.List; 14 15 public class TestMaker 16 { 17 @Test(description = "Make sure only one listener is created and not 2^3") run()18 public void run() 19 { 20 final TestNG tng = new TestNG(); 21 tng.setUseDefaultListeners(false); 22 tng.setListenerClasses(Arrays.<Class> asList(TestListenerAdapter.class, SimpleReporter.class)); 23 final List<XmlSuite> suites = createSuites(); 24 tng.setXmlSuites(suites); 25 tng.setVerbose(0); 26 tng.run(); 27 28 // Reporter.log(tng.getSuiteListeners().size() + "", true); 29 // for (final XmlSuite xmlSuite : suites) 30 // { 31 // Reporter.log(xmlSuite.getName() + ": " + xmlSuite.getListeners().size(), true); 32 // } 33 } 34 createSuites()35 private List<XmlSuite> createSuites() 36 { 37 final List<XmlSuite> ret = Lists.newArrayList(); 38 for (int i = 0; i < 3; i++) 39 { 40 ret.add(createSuite(i)); 41 } 42 return ret; 43 } 44 createSuite(final int nr)45 private XmlSuite createSuite(final int nr) 46 { 47 final XmlSuite suite = new XmlSuite(); 48 suite.setName("Suite_" + nr); 49 50 new XmlTest(suite).setXmlClasses(Collections.singletonList(new XmlClass(Test1.class))); 51 return suite; 52 } 53 } 54