1 package test.groupinvocation;
2 
3 import org.testng.TestListenerAdapter;
4 import org.testng.TestNG;
5 import org.testng.annotations.Test;
6 import org.testng.xml.XmlSuite;
7 import org.testng.xml.XmlTest;
8 
9 import test.SimpleBaseTest;
10 
11 import java.util.Arrays;
12 
13 /**
14  * Test that <suite> tags can have groups.
15  */
16 @Test
17 public class GroupSuiteTest extends SimpleBaseTest {
18 
includeFromSuite0()19   public void includeFromSuite0() {
20     runWithSuite(g(), g(), g("a", "b", "c"));
21   }
22 
includeFromSuite1()23   public void includeFromSuite1() {
24     runWithSuite(g("a"), g(), g("a"));
25   }
26 
includeFromSuite2()27   public void includeFromSuite2() {
28     runWithSuite(g("a", "b"), g(), g("a", "b"));
29   }
30 
excludeFromSuite1()31   public void excludeFromSuite1() {
32     runWithSuite(g(), g("a"), g("b", "c"));
33   }
34 
excludeFromSuite2()35   public void excludeFromSuite2() {
36     runWithSuite(g(), g("a", "b"), g("c"));
37   }
38 
39   @Test(description = "Include in both suite and test")
includeTestAndSuite1()40   public void includeTestAndSuite1() {
41     runWithSuite(g("a"), g(), g("b"), g(), g("a", "b"));
42   }
43 
44   @Test(description = "Include in suite, exclude in test")
excludeTestAndSuite2()45   public void excludeTestAndSuite2() {
46     runWithSuite(g(), g("a"), g(), g("a"), g("b", "c"));
47   }
48 
runWithSuite(String[] suiteGroups, String[] excludedSuiteGroups, String[] methods)49   private void runWithSuite(String[] suiteGroups, String[] excludedSuiteGroups,
50       String[] methods) {
51     runWithSuite(suiteGroups, excludedSuiteGroups, g(), g(), methods);
52   }
53 
runWithSuite(String[] suiteGroups, String[] excludedSuiteGroups, String[] testGroups, String[] excludedTestGroups, String[] methods)54   private void runWithSuite(String[] suiteGroups, String[] excludedSuiteGroups,
55       String[] testGroups, String[] excludedTestGroups,
56       String[] methods) {
57     XmlSuite s = createXmlSuite("Groups");
58     s.setIncludedGroups(Arrays.asList(suiteGroups));
59     s.setExcludedGroups(Arrays.asList(excludedSuiteGroups));
60     XmlTest t = createXmlTest(s, "Groups-test", GroupSuiteSampleTest.class.getName());
61     t.setIncludedGroups(Arrays.asList(testGroups));
62     t.setExcludedGroups(Arrays.asList(excludedTestGroups));
63     TestListenerAdapter tla = new TestListenerAdapter();
64     TestNG tng = create();
65     tng.addListener(tla);
66     tng.setXmlSuites(Arrays.asList(new XmlSuite[] { s }));
67     tng.run();
68 
69     verifyPassedTests(tla, methods);
70   }
71 
g(String... groups)72   private String[] g(String... groups) {
73     return groups;
74   }
75 }
76