1 package test.dependent.xml;
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.DataProvider;
8 import org.testng.annotations.Test;
9 import org.testng.xml.XmlSuite;
10 import org.testng.xml.XmlTest;
11 
12 import test.SimpleBaseTest;
13 
14 import java.util.Arrays;
15 import java.util.List;
16 
17 public class GroupDependencyTest extends SimpleBaseTest {
18   @DataProvider
dp()19   public Object[][] dp() {
20     return new Object[][] {
21       new Object[] { new String[] { "a", "b", "c", "a1", "b1", "c1" } },
22       new Object[] { new String[] { "a", "c", "b", "a1", "c1", "b1" } },
23       new Object[] { new String[] { "b", "a", "c", "b1", "a1", "c1" } },
24       new Object[] { new String[] { "b", "c", "a", "b1", "c1", "a1" } },
25       new Object[] { new String[] { "c", "b", "a", "c1", "b1", "a1" } },
26       new Object[] { new String[] { "c", "a", "b", "c1", "a1", "b1" } },
27     };
28   }
29 
30   @Test(dataProvider = "dp")
verifyGroupSingle(String[] a)31   public void verifyGroupSingle(String[] a) {
32     configureGroup(a, false /* single */);
33   }
34 
35   @Test(dataProvider = "dp")
verifyGroupMulti(String[] a)36   public void verifyGroupMulti(String[] a) {
37     configureGroup(a, true /* multi */);
38   }
39 
configureGroup(String[] a, boolean multi)40   private void configureGroup(String[] a, boolean multi) {
41     XmlSuite suite = createXmlSuite("Dependencies");
42     XmlTest test =
43         createXmlTest(suite, "DependencyTest", GroupDependencySampleTest.class.getName());
44     if (multi) {
45       test.addXmlDependencyGroup(a[2], a[1] + " " + a[0]);
46     } else {
47       test.addXmlDependencyGroup(a[2], a[1]);
48       test.addXmlDependencyGroup(a[1], a[0]);
49     }
50 
51     TestNG tng = create();
52     tng.setXmlSuites(Arrays.asList(suite));
53     TestListenerAdapter tla = new TestListenerAdapter();
54     tng.addListener(tla);
55     tng.run();
56 
57     List<ITestResult> t = tla.getPassedTests();
58     String method2 = t.get(2).getMethod().getMethodName();
59     if (multi) {
60       // When we have "a depends on groups b and c", the only certainty is that "a"
61       // will be run last
62       Assert.assertEquals(method2, a[5]);
63     } else {
64       assertTestResultsEqual(tla.getPassedTests(), Arrays.asList(a[3], a[4], a[5]));
65     }
66   }
67 }
68