1package test.commandline;
2
3import org.testng.TestListenerAdapter;
4import org.testng.TestNG;
5import org.testng.annotations.Test;
6import org.testng.xml.XmlSuite;
7import org.testng.xml.XmlTest;
8
9import test.SimpleBaseTest;
10
11import java.util.Arrays;
12import java.util.List;
13
14public class CommandLineOverridesXml extends SimpleBaseTest {
15
16  @Test(description = "Specifying -groups on the command line should override testng.xml")
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")
22  public void commandLineExcludedGroupsShouldOverrideXml() {
23    runTest(null, "go", Arrays.asList(new String[] { "f1" }));
24  }
25
26  @Test
27  public void shouldRunBothMethods() {
28    runTest(null, null, Arrays.asList(new String[] { "f1", "f2" }));
29  }
30
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