1 package test.simple; 2 3 import org.testng.Assert; 4 import org.testng.IReporter; 5 import org.testng.ISuite; 6 import org.testng.ITestNGMethod; 7 import org.testng.TestNG; 8 import org.testng.annotations.BeforeMethod; 9 import org.testng.annotations.Test; 10 import org.testng.xml.XmlSuite; 11 12 import testhelper.OutputDirectoryPatch; 13 14 import java.util.Collection; 15 import java.util.List; 16 17 public class IncludedExcludedTest { 18 19 private TestNG m_tng; 20 21 @BeforeMethod init()22 public void init() { 23 m_tng = new TestNG(); 24 m_tng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); 25 m_tng.setVerbose(0); 26 m_tng.setUseDefaultListeners(false); 27 } 28 29 @Test(description = "First test method") verifyIncludedExcludedCount1()30 public void verifyIncludedExcludedCount1() { 31 m_tng.setTestClasses(new Class[] {IncludedExcludedSampleTest.class}); 32 m_tng.setGroups("a"); 33 m_tng.addListener( 34 new MyReporter(new String[] { "test3" }, new String[] { "test1", "test2"})); 35 m_tng.run(); 36 } 37 38 @Test(description = "Second test method") verifyIncludedExcludedCount2()39 public void verifyIncludedExcludedCount2() { 40 m_tng.setTestClasses(new Class[] {IncludedExcludedSampleTest.class}); 41 m_tng.addListener( 42 new MyReporter( 43 new String[] { 44 "beforeSuite", "beforeTest", "beforeTestClass", 45 "beforeTestMethod", "test1", "beforeTestMethod", "test3" 46 }, 47 new String[] { "test2"})); 48 m_tng.run(); 49 } 50 51 } 52 53 class MyReporter implements IReporter { 54 55 private String[] m_included; 56 private String[] m_excluded; 57 MyReporter(String[] included, String[] excluded)58 public MyReporter(String[] included, String[] excluded) { 59 m_included = included; 60 m_excluded = excluded; 61 } 62 63 @Override generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory)64 public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) { 65 Assert.assertEquals(suites.size(), 1); 66 ISuite suite = suites.get(0); 67 68 { 69 Collection<ITestNGMethod> invoked = suite.getInvokedMethods(); 70 Assert.assertEquals(invoked.size(), m_included.length); 71 for (String s : m_included) { 72 Assert.assertTrue(containsMethod(invoked, s)); 73 } 74 } 75 76 { 77 Collection<ITestNGMethod> excluded = suite.getExcludedMethods(); 78 Assert.assertEquals(excluded.size(), m_excluded.length); 79 for (String s : m_excluded) { 80 Assert.assertTrue(containsMethod(excluded, s)); 81 } 82 } 83 } 84 containsMethod(Collection<ITestNGMethod> invoked, String string)85 private boolean containsMethod(Collection<ITestNGMethod> invoked, String string) { 86 for (ITestNGMethod m : invoked) { 87 if (m.getMethodName().equals(string)) { 88 return true; 89 } 90 } 91 92 return false; 93 } 94 95 } 96