Best Testng code snippet using org.testng.TestNG.privateMain
Source:FailurePolicyTest.java
...49 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(),50 "-configfailurepolicy", "skip",51 "-testclass", "test.configurationfailurepolicy.ClassWithFailedBeforeMethodAndMultipleTests" };52 TestListenerAdapter tla = new TestListenerAdapter();53 TestNG.privateMain(argv, tla);54 verify(tla, 1, 1, 2);55 }56 @Test57 public void commandLineTest_policyAsContinue() {58 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(),59 "-configfailurepolicy", "continue",60 "-testclass", "test.configurationfailurepolicy.ClassWithFailedBeforeMethodAndMultipleTests" };61 TestListenerAdapter tla = new TestListenerAdapter();62 TestNG.privateMain(argv, tla);63 verify(tla, 2, 0, 2);64 }65 @Test66 public void commandLineTestWithXMLFile_policyAsSkip() {67 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(),68 "-configfailurepolicy", "skip", "src/test/resources/testng-configfailure.xml" };69 TestListenerAdapter tla = new TestListenerAdapter();70 TestNG.privateMain(argv, tla);71 verify(tla, 1, 1, 2);72 }73 @Test74 public void commandLineTestWithXMLFile_policyAsContinue() {75 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(),76 "-configfailurepolicy", "continue", "src/test/resources/testng-configfailure.xml" };77 TestListenerAdapter tla = new TestListenerAdapter();78 TestNG.privateMain(argv, tla);79 verify(tla, 2, 0, 2);80 }81 private void verify( TestListenerAdapter tla, int configurationFailures, int configurationSkips, int skippedTests ) {82 assertEquals(tla.getConfigurationFailures().size(), configurationFailures, "wrong number of configuration failures");83 assertEquals(tla.getConfigurationSkips().size(), configurationSkips, "wrong number of configuration skips");84 assertEquals(tla.getSkippedTests().size(), skippedTests, "wrong number of skipped tests");85 }86}...
Source:CommandLineTest.java
...22 "-junit",23 "-testclass", "test.sample.JUnitSample1"24 };25 TestListenerAdapter tla = new TestListenerAdapter();26 TestNG.privateMain(argv, tla);27 List<ITestResult> passed = tla.getPassedTests();28 assertEquals(passed.size(), 2);29 String test1 = passed.get(0).getMethod().getMethodName();30 String test2 = passed.get(1).getMethod().getMethodName();31 assertTrue(JUnitSample1.EXPECTED1.equals(test1) && JUnitSample1.EXPECTED2.equals(test2) ||32 JUnitSample1.EXPECTED1.equals(test2) && JUnitSample1.EXPECTED2.equals(test1));33 }34 /**35 * Test the absence of -junit36 */37 @Test(groups = { "current" } )38 public void junitParsing2() {39 String[] argv = {40 "-log", "0",41 "-d", OutputDirectoryPatch.getOutputDirectory(),42 "-testclass", "test.sample.JUnitSample1"43 };44 TestListenerAdapter tla = new TestListenerAdapter();45 TestNG.privateMain(argv, tla);46 List<ITestResult> passed = tla.getPassedTests();47 assertEquals(passed.size(), 0);48 }49 /**50 * Test the ability to override the default command line Suite name51 */52 @Test(groups = { "current" } )53 public void suiteNameOverride() {54 String suiteName="MySuiteName";55 String[] argv = {56 "-log", "0",57 "-d", OutputDirectoryPatch.getOutputDirectory(),58 "-junit",59 "-testclass", "test.sample.JUnitSample1",60 "-suitename", "\""+suiteName+"\""61 };62 TestListenerAdapter tla = new TestListenerAdapter();63 TestNG.privateMain(argv, tla);64 List<ITestContext> contexts = tla.getTestContexts();65 assertTrue(contexts.size()>0);66 for (ITestContext context:contexts) {67 assertEquals(context.getSuite().getName(),suiteName);68 }69 }70 /**71 * Test the ability to override the default command line test name72 */73 @Test(groups = { "current" } )74 public void testNameOverride() {75 String testName="My Test Name";76 String[] argv = {77 "-log", "0",78 "-d", OutputDirectoryPatch.getOutputDirectory(),79 "-junit",80 "-testclass", "test.sample.JUnitSample1",81 "-testname", "\""+testName+"\""82 };83 TestListenerAdapter tla = new TestListenerAdapter();84 TestNG.privateMain(argv, tla);85 List<ITestContext> contexts = tla.getTestContexts();86 assertTrue(contexts.size()>0);87 for (ITestContext context:contexts) {88 assertEquals(context.getName(),testName);89 }90 }91 @Test92 public void testUseDefaultListenersArgument() {93 TestNG.privateMain(new String[] {94 "-log", "0", "-usedefaultlisteners", "false", "-testclass", "test.sample.JUnitSample1"95 }, null);96 }97 @Test98 public void testMethodParameter() {99 String[] argv = {100 "-log", "0",101 "-d", OutputDirectoryPatch.getOutputDirectory(),102 "-methods", "test.sample.Sample2.method1,test.sample.Sample2.method3",103 };104 TestListenerAdapter tla = new TestListenerAdapter();105 TestNG.privateMain(argv, tla);106 List<ITestResult> passed = tla.getPassedTests();107 Assert.assertEquals(passed.size(), 2);108 Assert.assertTrue((passed.get(0).getName().equals("method1") &&109 passed.get(1).getName().equals("method3"))110 ||111 (passed.get(1).getName().equals("method1") &&112 passed.get(0).getName().equals("method3")));113 }114 private static void ppp(String s) {115 System.out.println("[CommandLineTest] " + s);116 }117}...
privateMain
Using AI Code Generation
1package com.example;2import org.testng.TestNG;3import java.util.Arrays;4public class Main {5 public static void main(String[] args) {6 TestNG testNG = new TestNG();7 testNG.setTestClasses(new Class[] { Test1.class, Test2.class });8 testNG.setOutputDirectory("test-output");9 testNG.run();10 }11}12package com.example;13import org.testng.annotations.Test;14public class Test1 {15 public void test1() {16 System.out.println("Test1");17 }18}19package com.example;20import org.testng.annotations.Test;21public class Test2 {22 public void test2() {23 System.out.println("Test2");24 }25}
privateMain
Using AI Code Generation
1import org.testng.TestNG;2public class TestNGRunner {3 public static void main(String[] args) {4 TestNG testng = new TestNG();5 testng.setTestClasses(new Class[] { TestClass.class });6 testng.run();7 }8}9package com.mkyong.testng;10import org.testng.annotations.Test;11public class TestClass {12 public void testMethod1() {13 System.out.println("testMethod1");14 }15 public void testMethod2() {16 System.out.println("testMethod2");17 }18}
privateMain
Using AI Code Generation
1import org.testng.TestNG2import org.testng.xml.XmlSuite3def suite = new XmlSuite()4suite.setParallel(XmlSuite.ParallelMode.CLASSES)5suite.setThreadCount(4)6def test1 = new XmlSuite.XmlTest(suite)7test1.setPreserveOrder(true)8test1.setParallel(XmlSuite.ParallelMode.METHODS)9test1.setThreadCount(2)10test1.addIncludedGroup("group1")11test1.addIncludedGroup("group2")12def test2 = new XmlSuite.XmlTest(suite)13test2.setPreserveOrder(true)14test2.setParallel(XmlSuite.ParallelMode.METHODS)15test2.setThreadCount(2)16test2.addIncludedGroup("group3")17test2.addIncludedGroup("group4")18def testng = new TestNG()19testng.setXmlSuites(Arrays.asList(suite))20testng.privateMain()21plugins {22}23plugins {24 id("org.testng") version "7.0.0"25}
privateMain
Using AI Code Generation
1import org.testng.TestNG;2import org.testng.xml.Parser;3import org.testng.xml.XmlSuite;4import java.io.File;5import java.util.Arrays;6import java.util.List;7public class TestNGRunner {8 public static void main(String[] args) {9 TestNG runner = new TestNG();10 runner.setVerbose(1);11 List<String> suites = Arrays.asList("testng.xml");12 runner.setTestSuites(suites);13 runner.run();14 }15}
privateMain
Using AI Code Generation
1at org.testng.TestNG.privateMain(TestNG.java:1063)2at org.testng.TestNG.privateMain(TestNG.java:1049)3at org.testng.TestNG.main(TestNG.java:1017)4at java.net.URLClassLoader$1.run(URLClassLoader.java:202)5at java.security.AccessController.doPrivileged(Native Method)6at java.net.URLClassLoader.findClass(URLClassLoader.java:190)7at java.lang.ClassLoader.loadClass(ClassLoader.java:306)8at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)9at java.lang.ClassLoader.loadClass(ClassLoader.java:247)10public static void main(String[] args) {11 TestNG.main(new String[] { "C:\\testng.xml" });12}13public static void main(String[] args) {14 TestNG testng = new TestNG();15 testng.setTestClasses(new Class[] { TestClass.class });16 testng.privateMain(new String[] { "C:\\testng.xml" });17}18public static void main(String[] args) {19 TestNG testng = new TestNG();20 testng.setTestClasses(new Class[] { TestClass.class });21 testng.privateMain(new String[] { "C:\\testng.xml" });22}23at org.testng.TestNG.privateMain(TestNG.java:1063)24at org.testng.TestNG.privateMain(TestNG.java:1049)25at org.testng.TestNG.main(TestNG.java:1017)
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!!