Best Testng code snippet using org.testng.TestNG.setGroups
Source:ArtifactCollectorTest.java
...24 TestNG testNG = new TestNG();25 testNG.setTestClasses(new Class[]{ArtifactCollectorTestCases.class});26 testNG.addListener((ITestNGListener) lc);27 testNG.addListener((ITestNGListener) tla);28 testNG.setGroups("testPassed");29 testNG.run();30 31 assertEquals(tla.getPassedTests().size(), 1, "Incorrect passed test count");32 assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count");33 assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count");34 assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count");35 assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count");36 assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count");37 38 ITestResult result = tla.getPassedTests().get(0);39 assertNull(UnitTestArtifact.getCaptureState(result), "Artifact provider capture state should be 'null'");40 assertNull(UnitTestCapture.getArtifactPath(result), "Artifact capture should not have been requested");41 }42 43 @Test44 public void verifyCaptureOnFailure() {45 46 ListenerChain lc = new ListenerChain();47 TestListenerAdapter tla = new TestListenerAdapter();48 49 TestNG testNG = new TestNG();50 testNG.setTestClasses(new Class[]{ArtifactCollectorTestCases.class});51 testNG.addListener((ITestNGListener) lc);52 testNG.addListener((ITestNGListener) tla);53 testNG.setGroups("testFailed");54 testNG.run();55 56 assertEquals(tla.getPassedTests().size(), 0, "Incorrect passed test count");57 assertEquals(tla.getFailedTests().size(), 1, "Incorrect failed test count");58 assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count");59 assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count");60 assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count");61 assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count");62 63 ITestResult result = tla.getFailedTests().get(0);64 assertEquals(UnitTestArtifact.getCaptureState(result), CaptureState.CAPTURE_SUCCESS, "Incorrect artifact provider capture state");65 assertTrue(UnitTestCapture.getArtifactPath(result).isPresent(), "Artifact capture output path is not present");66 }67 68 @Test69 public void verifyCanNotCapture() {70 71 ListenerChain lc = new ListenerChain();72 TestListenerAdapter tla = new TestListenerAdapter();73 74 TestNG testNG = new TestNG();75 testNG.setTestClasses(new Class[]{ArtifactCollectorTestCases.class});76 testNG.addListener((ITestNGListener) lc);77 testNG.addListener((ITestNGListener) tla);78 testNG.setGroups("canNotCapture");79 testNG.run();80 81 assertEquals(tla.getPassedTests().size(), 0, "Incorrect passed test count");82 assertEquals(tla.getFailedTests().size(), 1, "Incorrect failed test count");83 assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count");84 assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count");85 assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count");86 assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count");87 88 ITestResult result = tla.getFailedTests().get(0);89 assertEquals(UnitTestArtifact.getCaptureState(result), CaptureState.CAN_NOT_CAPTURE, "Incorrect artifact provider capture state");90 assertFalse(UnitTestCapture.getArtifactPath(result).isPresent(), "Artifact capture output path should not be present");91 }92 93 @Test94 public void verifyWillNotCapture() {95 96 ListenerChain lc = new ListenerChain();97 TestListenerAdapter tla = new TestListenerAdapter();98 99 TestNG testNG = new TestNG();100 testNG.setTestClasses(new Class[]{ArtifactCollectorTestCases.class});101 testNG.addListener((ITestNGListener) lc);102 testNG.addListener((ITestNGListener) tla);103 testNG.setGroups("willNotCapture");104 testNG.run();105 106 assertEquals(tla.getPassedTests().size(), 0, "Incorrect passed test count");107 assertEquals(tla.getFailedTests().size(), 1, "Incorrect failed test count");108 assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count");109 assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count");110 assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count");111 assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count");112 113 ITestResult result = tla.getFailedTests().get(0);114 assertEquals(UnitTestArtifact.getCaptureState(result), CaptureState.CAPTURE_FAILED, "Incorrect artifact provider capture state");115 assertFalse(UnitTestCapture.getArtifactPath(result).isPresent(), "Artifact capture output path should not be present");116 }117 118 @Test119 public void verifyOnDemandCapture() {120 121 ListenerChain lc = new ListenerChain();122 TestListenerAdapter tla = new TestListenerAdapter();123 124 TestNG testNG = new TestNG();125 testNG.setTestClasses(new Class[]{ArtifactCollectorTestCases.class});126 testNG.addListener((ITestNGListener) lc);127 testNG.addListener((ITestNGListener) tla);128 testNG.setGroups("onDemandCapture");129 testNG.run();130 131 assertEquals(tla.getPassedTests().size(), 1, "Incorrect passed test count");132 assertEquals(tla.getFailedTests().size(), 0, "Incorrect failed test count");133 assertEquals(tla.getSkippedTests().size(), 0, "Incorrect skipped test count");134 assertEquals(tla.getFailedButWithinSuccessPercentageTests().size(), 0, "Incorrect curve-graded success count");135 assertEquals(tla.getConfigurationFailures().size(), 0, "Incorrect configuration method failure count");136 assertEquals(tla.getConfigurationSkips().size(), 0, "Incorrect configuration method skip count");137 138 ITestResult result = tla.getPassedTests().get(0);139 assertEquals(UnitTestArtifact.getCaptureState(result), CaptureState.CAPTURE_SUCCESS, "Incorrect artifact provider capture state");140 assertTrue(UnitTestCapture.getArtifactPath(result).isPresent(), "Artifact capture output path is not present");141 }142}
...
Source:OverrideTest.java
...29 );30 TestNG tng = create();31 TestListenerAdapter tla = new TestListenerAdapter();32 tng.addListener((ITestNGListener) tla);33 if (include != null) tng.setGroups(include);34 if (exclude != null) tng.setExcludedGroups(exclude);35 tng.setTestSuites(Collections.singletonList(f.getAbsolutePath()));36 tng.run();37 Assert.assertEquals(tla.getPassedTests().size(), 1);38 }39 @Test(description = "Override -groups")40 public void overrideIncludeShouldWork()41 throws ParserConfigurationException, SAXException, IOException {42 runTest("goodGroup", null);43 }44 @Test(description = "Override -excludegroups")45 public void overrideExcludeShouldWork()46 throws ParserConfigurationException, SAXException, IOException {47 runTest(null, "badGroup");...
Source:ConfigurationGroupsTest.java
...7public class ConfigurationGroupsTest extends SimpleBaseTest {8 @Test9 public void multipleBeforeGroupTest() {10 TestNG testng = create(MultipleBeforeGroupTest.class);11 testng.setGroups("foo");12 testng.run();13 assertThat(testng.getStatus()).isEqualTo(0);14 }15 @Test(dataProvider = "getTestData")16 public void runTest(Class<?> testClass, String groupName) {17 TestNG testng = create(testClass);18 testng.setGroups(groupName);19 testng.run();20 assertThat(testng.getStatus()).isEqualTo(0);21 }22 @DataProvider23 public Object[][] getTestData() {24 return new Object[][]{25 {ConfigurationGroups1SampleTest.class, "cg1-a, cg1-1"},26 {ConfigurationGroups2SampleTest.class, "cg2-a,cg2-1"},27 {ConfigurationGroups3SampleTest.class, "cg34-a, cg34-1"},28 {ConfigurationGroups4SampleTest.class, "cg4-1"},29 {ConfigurationGroups5SampleTest.class, "cg5-1, cg5-2"},30 {ConfigurationGroups6SampleTest.class, "cg6-1"},31 {ConfigurationGroups7SampleTest.class, "A"}32 };...
Source:AbstractTestNGCucumberTestsTest.java
...11 public void setUp() {12 InvokedConfigurationMethodListener icml = new InvokedConfigurationMethodListener();13 TestNG testNG = new TestNG();14 testNG.addListener(icml);15 testNG.setGroups("cucumber");16 testNG.setTestClasses(new Class[]{new AbstractTestNGCucumberTests() {}.getClass()});17 testNG.run();18 invokedConfigurationMethodNames = icml.getInvokedMethodNames();19 }20 21 @Test22 public void setUpClassIsInvoked() {23 Assert.assertTrue(invokedConfigurationMethodNames.contains("setUpClass"), "setUpClass must be invoked");24 }25 26 @Test27 public void tearDownClassIsInvoked() {28 Assert.assertTrue(invokedConfigurationMethodNames.contains("tearDownClass"), "tearDownClass must be invoked");29 }...
Source:DataProviderThreadPoolSizeTest.java
...6public class DataProviderThreadPoolSizeTest extends SimpleBaseTest {7 @Test8 public void shouldUseDefaultDataProviderThreadCount() {9 TestNG tng = create(DataProviderThreadPoolSizeSampleTest.class);10 tng.setGroups("parallel");11 tng.run();12 Assert.assertEquals(DataProviderThreadPoolSizeSampleTest.getThreadCount(), 10);13 }14 @Test15 public void shouldNotUseThreadsIfNotUsingParallel() {16 TestNG tng = create(DataProviderThreadPoolSizeSampleTest.class);17 tng.setGroups("sequential");18 tng.run();19 Assert.assertEquals(DataProviderThreadPoolSizeSampleTest.getThreadCount(), 1);20 }21 @Test22 public void shouldUseSpecifiedDataProviderThreadCount() {23 TestNG tng = create(DataProviderThreadPoolSizeSampleTest.class);24 tng.setGroups("parallel");25 tng.setDataProviderThreadCount(3);26 tng.run();27 Assert.assertEquals(DataProviderThreadPoolSizeSampleTest.getThreadCount(), 3);28 }29}...
Source:RayAlterSuiteListener.java
...18 if (!"1".equals(System.getenv("ENABLE_MULTI_LANGUAGE_TESTS"))) {19 run.onExclude("multiLanguage");20 }21 groups.setRun(run);22 suite.setGroups(groups);23 }24}...
setGroups
Using AI Code Generation
1package testng;2import java.util.ArrayList;3import java.util.List;4import org.testng.TestNG;5public class TestNGSetGroups {6 public static void main(String[] args) {7 TestNG runner = new TestNG();8 List<String> suitefiles = new ArrayList<String>();9 suitefiles.add("C:\\Users\\Srikanth\\workspace\\TestNG\\testng.xml");10 runner.setTestSuites(suitefiles);11 runner.setGroups("test");12 runner.run();13 }14}
setGroups
Using AI Code Generation
1TestNG testng = new TestNG();2testng.setGroups("group1,group2");3testng.setTestClasses(new Class[] { TestClass1.class, TestClass2.class });4testng.run();5XmlSuite suite = new XmlSuite();6suite.setName("Sample Suite");7suite.setGroups("group1,group2");8XmlTest test = new XmlTest(suite);9test.setName("Sample Test");10test.setXmlClasses(Arrays.asList(new XmlClass(TestClass1.class), new XmlClass(TestClass2.class)));11TestNG tng = new TestNG();12tng.setXmlSuites(Arrays.asList(suite));13tng.run();14XmlSuite suite = new XmlSuite();15suite.setName("Sample Suite");16XmlTest test = new XmlTest(suite);17test.setName("Sample Test");18test.setGroups("group1,group2");19test.setXmlClasses(Arrays.asList(new XmlClass(TestClass1.class), new XmlClass(TestClass2.class)));20TestNG tng = new TestNG();21tng.setXmlSuites(Arrays.asList(suite));22tng.run();23XmlSuite suite = new XmlSuite();24suite.setName("Sample Suite");25XmlTest test = new XmlTest(suite);26test.setName("Sample Test");27test.setXmlClasses(Arrays.asList(new XmlClass(TestClass1.class).setGroups("group1,group2"), new XmlClass(TestClass2.class).setGroups("group1,group2")));28TestNG tng = new TestNG();29tng.setXmlSuites(Arrays.asList(suite));30tng.run();31XmlSuite suite = new XmlSuite();32suite.setName("Sample Suite");33XmlTest test = new XmlTest(suite);34test.setName("Sample Test");35test.setXmlClasses(Arrays.asList(new XmlClass(TestClass1.class).setIncludedMethods(Arrays.asList(new XmlInclude("test1").setGroups("group1,group2"), new XmlInclude("test2").setGroups("group1,group2"))), new XmlClass(TestClass2.class).setIncludedMethods(Arrays.asList(new XmlInclude("test1").setGroups("group1,group2"), new XmlInclude("test2").setGroups("group1,group2")))));
setGroups
Using AI Code Generation
1TestNG testng = new TestNG();2testng.setGroups("group1,group2");3testng.setTestClasses(new Class[] { TestClass1.class, TestClass2.class });4testng.run();5XmlSuite suite = new XmlSuite();6suite.setName("Suite");7suite.setParallel(XmlSuite.ParallelMode.METHODS);8suite.setThreadCount(3);9suite.setVerbose(1);10suite.setGroupByInstances(true);11suite.setPreserveOrder(true);12List<XmlClass> classes = new ArrayList<XmlClass>();13classes.add(new XmlClass("test.TestClass1"));14classes.add(new XmlClass("test.TestClass2"));15List<XmlTest> tests = new ArrayList<XmlTest>();16XmlTest test = new XmlTest(suite);17test.setName("Test");18test.setXmlClasses(classes);19test.setPreserveOrder(true);20test.setGroups("group1,group2");21tests.add(test);22suite.setTests(tests);23TestNG tng = new TestNG();24tng.setXmlSuites(Arrays.asList(suite));25tng.run();26XmlSuite suite = new XmlSuite();27suite.setName("Suite");28suite.setParallel(XmlSuite.ParallelMode.METHODS);29suite.setThreadCount(3);30suite.setVerbose(1);31suite.setGroupByInstances(true);32suite.setPreserveOrder(true);33List<XmlClass> classes = new ArrayList<XmlClass>();34classes.add(new XmlClass("test.TestClass1"));35classes.add(new XmlClass("test.TestClass2"));36List<XmlTest> tests = new ArrayList<XmlTest>();37XmlTest test = new XmlTest(suite);38test.setName("Test");39test.setXmlClasses(classes);40test.setPreserveOrder(true);41test.setGroups("group1,group2");42tests.add(test);43suite.setTests(tests);44TestNG tng = new TestNG();45tng.setXmlSuites(Arrays.asList(suite));46tng.run();47XmlSuite suite = new XmlSuite();48suite.setName("Suite");49suite.setParallel(XmlSuite.ParallelMode.METHODS);50suite.setThreadCount(3);51suite.setVerbose(1);52suite.setGroupByInstances(true);53suite.setPreserveOrder(true);54List<XmlClass> classes = new ArrayList<XmlClass>();55classes.add(new XmlClass
setGroups
Using AI Code Generation
1import org.testng.TestNG2import org.testng.xml.XmlSuite3import org.testng.xml.XmlTest4import org.testng.xml.XmlClass5import org.testng.xml.XmlGroups6def testng = new TestNG()7def xmlSuite = new XmlSuite()8def xmlTest = new XmlTest(xmlSuite)9def xmlClass = new XmlClass('com.example.SampleTest')10def xmlGroups = new XmlGroups()11xmlGroups.setIncludedGroups(['group1', 'group2'])12xmlTest.setGroups(xmlGroups)13xmlTest.setXmlClasses([xmlClass])14testng.setXmlSuites([xmlSuite])15testng.run()16import org.testng.TestNG17import org.testng.xml.XmlSuite18import org.testng.xml.XmlTest19import org.testng.xml.XmlClass20import org.testng.xml.XmlGroups21def testng = new TestNG()22def xmlSuite = new XmlSuite()23def xmlTest = new XmlTest(xmlSuite)24def xmlClass = new XmlClass('com.example.SampleTest')25def xmlGroups = new XmlGroups()26xmlGroups.setIncludedGroups(['group1', 'group2'])27xmlTest.setGroups(xmlGroups)28xmlTest.setXmlClasses([xmlClass])29testng.setXmlSuites([xmlSuite])30testng.run()31import org.testng.TestNG32import org.testng.xml.XmlSuite33import org.testng.xml.XmlTest34import org.testng.xml.XmlClass35import org.testng.xml.XmlGroups36def testng = new TestNG()37def xmlSuite = new XmlSuite()38def xmlTest = new XmlTest(xmlSuite)39def xmlClass = new XmlClass('com.example.SampleTest')40def xmlGroups = new XmlGroups()41xmlGroups.setIncludedGroups(['group1', 'group2'])42xmlClass.setGroups(xmlGroups)43xmlTest.setXmlClasses([xmlClass])44testng.setXmlSuites([xmlSuite])45testng.run()46import org.testng.TestNG47import org.testng.xml.XmlSuite48import org.testng.xml.XmlTest49import org.testng.xml.XmlClass50import org.testng.xml.XmlGroups51def testng = new TestNG()52def xmlSuite = new XmlSuite()53def xmlTest = new XmlTest(xmlSuite)
setGroups
Using AI Code Generation
1import org.testng.TestNG;2import org.testng.xml.XmlSuite;3import java.util.ArrayList;4import java.util.List;5public class RunTestsInParallel {6 public static void main(String[] args) {7 TestNG testNG = new TestNG();8 XmlSuite suite = new XmlSuite();9 suite.setName("TestNG Parallel");10 XmlSuite.XmlTest test = new XmlSuite.XmlTest(suite);11 test.setName("TestNG Parallel Test");12 test.setPreserveOrder("true");13 List<String> classes = new ArrayList<String>();14 classes.add("testng.TestNGTest");15 test.setXmlClasses(classes);16 List<XmlSuite.XmlTest> tests = new ArrayList<XmlSuite.XmlTest>();17 tests.add(test);18 suite.setTests(tests);19 List<XmlSuite> suites = new ArrayList<XmlSuite>();20 suites.add(suite);21 testNG.setXmlSuites(suites);22 List<String> suiteFiles = new ArrayList<String>();23 suiteFiles.add("testng.xml");24 testNG.setTestSuites(suiteFiles);25 testNG.run();26 }27}28org.testng.TestNG class setGroups() method29public void setGroups(String groups)
TestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.
You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.
Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!