1package test.override; 2 3import org.testng.Assert; 4import org.testng.TestListenerAdapter; 5import org.testng.TestNG; 6import org.testng.annotations.Test; 7import org.testng.internal.Utils; 8import org.xml.sax.SAXException; 9 10import test.SimpleBaseTest; 11 12import javax.xml.parsers.ParserConfigurationException; 13 14import java.io.File; 15import java.io.IOException; 16import java.util.Arrays; 17 18/** 19 * Verify that command line switches override parameters in testng.xml. 20 * 21 * @author Cedric Beust <cedric@beust.com> 22 */ 23public class OverrideTest extends SimpleBaseTest { 24 25 private void runTest(String include, String exclude) { 26 File f = Utils.createTempFile( 27 "<suite name=\"S\">" 28 + " <test name=\"T\">" 29 + " <classes>" 30 + " <class name=\"test.override.OverrideSampleTest\" />" 31 + " </classes>" 32 + " </test>" 33 + "</suite>" 34 ); 35 TestNG tng = create(); 36 TestListenerAdapter tla = new TestListenerAdapter(); 37 tng.addListener(tla); 38 if (include != null) tng.setGroups(include); 39 if (exclude != null) tng.setExcludedGroups(exclude); 40 tng.setTestSuites(Arrays.asList(f.getAbsolutePath())); 41 tng.run(); 42 43 Assert.assertEquals(tla.getPassedTests().size(), 1); 44 } 45 46 @Test(description = "Override -groups") 47 public void overrideIncludeShouldWork() 48 throws ParserConfigurationException, SAXException, IOException { 49 runTest("goodGroup", null); 50 } 51 52 @Test(description = "Override -excludegroups") 53 public void overrideExcludeShouldWork() 54 throws ParserConfigurationException, SAXException, IOException { 55 runTest(null, "badGroup"); 56 } 57 58 @Test(description = "Override -groups and -excludegroups") 59 public void overrideIncludeAndExcludeShouldWork() 60 throws ParserConfigurationException, SAXException, IOException { 61 runTest("goodGroup", "badGroup"); 62 } 63} 64