1package test.configuration; 2 3import org.testng.Assert; 4import org.testng.Reporter; 5import org.testng.TestListenerAdapter; 6import org.testng.TestNG; 7import org.testng.annotations.BeforeSuite; 8import org.testng.annotations.Test; 9import org.testng.xml.XmlClass; 10import org.testng.xml.XmlSuite; 11import org.testng.xml.XmlTest; 12 13import test.TestHelper; 14 15import java.util.ArrayList; 16import java.util.Arrays; 17import java.util.List; 18 19public class BeforeTestOrderingTest { 20 private static List<String> m_testNames; 21 22 @BeforeSuite 23 public void init() { 24 m_testNames = new ArrayList<>(); 25 } 26 27 static void addTest(String testName) { 28 m_testNames.add(testName); 29 } 30 31 @Test 32 public void verifyBeforeTestOrdering() { 33 34 XmlSuite s = new XmlSuite(); 35 36 Reporter.log("BEFORE"); 37 38 XmlTest t1 = new XmlTest(s); 39 XmlClass c1 = new XmlClass("test.configuration.BeforeTestOrdering1Test"); 40 t1.getXmlClasses().add(c1); 41 42 XmlTest t2 = new XmlTest(s); 43 XmlClass c2 = new XmlClass("test.configuration.BeforeTestOrdering2Test"); 44 t2.getXmlClasses().add(c2); 45 46 TestNG tng = TestHelper.createTestNG(); 47 TestListenerAdapter tl = new TestListenerAdapter(); 48 tng.addListener(tl); 49 tng.setXmlSuites(Arrays.asList(new XmlSuite[] { s })); 50 tng.run(); 51 52 List<String> expected = Arrays.asList(new String[] { 53 "bt1", "f1", "at1", "bt2", "f2", "at2", 54 }); 55 56 Assert.assertEquals(expected, m_testNames); 57 } 58 59 60 61 private static void ppp(String s) { 62 System.out.println("[BeforeTestOrderingTest] " + s); 63 } 64 65} 66