1 package test.methodselectors; 2 3 import org.testng.Assert; 4 import org.testng.ITestResult; 5 import org.testng.TestListenerAdapter; 6 import org.testng.TestNG; 7 import org.testng.annotations.BeforeMethod; 8 import org.testng.annotations.Test; 9 import org.testng.collections.Lists; 10 import org.testng.xml.XmlClass; 11 import org.testng.xml.XmlMethodSelector; 12 import org.testng.xml.XmlSuite; 13 import org.testng.xml.XmlTest; 14 15 import test.SimpleBaseTest; 16 import testhelper.OutputDirectoryPatch; 17 18 import java.util.Arrays; 19 import java.util.List; 20 21 public class MethodSelectorInSuiteTest extends SimpleBaseTest{ 22 23 private TestListenerAdapter m_tla; 24 25 @BeforeMethod setup()26 public void setup() { 27 m_tla = new TestListenerAdapter(); 28 } 29 30 @Test programmaticXmlSuite()31 public void programmaticXmlSuite() { 32 TestNG tng = create(); 33 XmlSuite suite = new XmlSuite(); 34 XmlMethodSelector methodSelector = new XmlMethodSelector(); 35 methodSelector.setName("test.methodselectors.Test2MethodSelector"); 36 methodSelector.setPriority(-1); 37 List<XmlMethodSelector> methodSelectors = Lists.newArrayList(); 38 methodSelectors.add(methodSelector); 39 suite.setMethodSelectors(methodSelectors); 40 XmlTest test = new XmlTest(suite); 41 XmlClass testClass = new XmlClass(test.methodselectors.SampleTest.class); 42 test.setXmlClasses(Arrays.asList(testClass)); 43 tng.setXmlSuites(Arrays.asList(suite)); 44 tng.addListener(m_tla); 45 tng.run(); 46 47 validate(new String[] { "test2" }); 48 } 49 50 @Test xmlXmlSuite()51 public void xmlXmlSuite() { 52 TestNG tng = create(); 53 tng.setTestSuites(Arrays.asList(getPathToResource("methodselector-in-xml.xml"))); 54 tng.addListener(m_tla); 55 tng.run(); 56 57 validate(new String[] { "test2" }); 58 } 59 60 @Test fileOnCommandLine()61 public void fileOnCommandLine() { 62 String[] args = new String[] { 63 "-d", OutputDirectoryPatch.getOutputDirectory(), 64 getPathToResource("methodselector-in-xml.xml") }; 65 TestNG.privateMain(args, m_tla); 66 67 validate(new String[] { "test2" }); 68 } 69 validate(String[] expectPassed)70 private void validate(String[] expectPassed) { 71 List<ITestResult> passed = m_tla.getPassedTests(); 72 Assert.assertEquals(passed.size(), expectPassed.length); 73 // doing this index based is probably not the best 74 for (int i = 0; i < expectPassed.length; i++) { 75 Assert.assertEquals(passed.get(i).getName(), expectPassed[i]); 76 } 77 } 78 } 79