1 package test.commandline;
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 import java.util.List;
13 
14 public class CommandLineOverridesXml extends SimpleBaseTest {
15 
16   @Test(description = "Specifying -groups on the command line should override testng.xml")
commandLineGroupsShouldOverrideXml()17   public void commandLineGroupsShouldOverrideXml() {
18     runTest("go", null, Arrays.asList(new String[] { "f2" }));
19   }
20 
21   @Test(description = "Specifying -excludegroups on the command line should override testng.xml")
commandLineExcludedGroupsShouldOverrideXml()22   public void commandLineExcludedGroupsShouldOverrideXml() {
23     runTest(null, "go", Arrays.asList(new String[] { "f1" }));
24   }
25 
26   @Test
shouldRunBothMethods()27   public void shouldRunBothMethods() {
28     runTest(null, null, Arrays.asList(new String[] { "f1", "f2" }));
29   }
30 
runTest(String group, String excludedGroups, List<String> methods)31   private void runTest(String group, String excludedGroups, List<String> methods) {
32     XmlSuite s = createXmlSuite(getClass().getName());
33     XmlTest t = createXmlTest(s, "Test", OverrideSampleTest.class.getName());
34     TestNG tng = create();
35     if (group != null) tng.setGroups(group);
36     if (excludedGroups != null) tng.setExcludedGroups(excludedGroups);
37     tng.setXmlSuites(Arrays.asList(new XmlSuite[] { s }));
38     TestListenerAdapter tla = new TestListenerAdapter();
39     tng.addListener(tla);
40     tng.run();
41 
42     assertTestResultsEqual(tla.getPassedTests(), methods);
43   }
44 }
45