Best Testng code snippet using org.testng.TestRunner.createWorkers
Source:TestRunner.java
...620 if (graph.getNodeCount() > 0 && freeNodes.isEmpty()) {621 throw new TestNGException("No free nodes found in:" + graph);622 }623 while (!freeNodes.isEmpty()) {624 List<IWorker<ITestNGMethod>> runnables = createWorkers(freeNodes);625 for (IWorker<ITestNGMethod> r : runnables) {626 r.run();627 }628 graph.setStatus(freeNodes, Status.FINISHED);629 freeNodes = graph.getFreeNodes();630 }631 }632 }633 }634 /** Apply the method interceptor (if applicable) to the list of methods. */635 private ITestNGMethod[] intercept(ITestNGMethod[] methods) {636 List<IMethodInstance> methodInstances =637 MethodHelper.methodsToMethodInstances(Arrays.asList(methods));638 for (IMethodInterceptor m_methodInterceptor : m_methodInterceptors) {639 methodInstances = m_methodInterceptor.intercept(methodInstances, this);640 }641 List<ITestNGMethod> result = MethodHelper.methodInstancesToMethods(methodInstances);642 // Since an interceptor is involved, we would need to ensure that the ClassMethodMap object is643 // in sync with the644 // output of the interceptor, else @AfterClass doesn't get executed at all when interceptors are645 // involved.646 // so let's update the current classMethodMap object with the list of methods obtained from the647 // interceptor.648 this.m_classMethodMap = new ClassMethodMap(result, null);649 ITestNGMethod[] resultArray = result.toArray(new ITestNGMethod[0]);650 // Check if an interceptor had altered the effective test method count. If yes, then we need to651 // update our configurationGroupMethod object with that information.652 if (resultArray.length != m_groupMethods.getAllTestMethods().length) {653 m_groupMethods =654 new ConfigurationGroupMethods(655 resultArray,656 m_groupMethods.getBeforeGroupsMethods(),657 m_groupMethods.getAfterGroupsMethods());658 }659 return resultArray;660 }661 /**662 * Create a list of workers to run the methods passed in parameter. Each test method is run in its663 * own worker except in the following cases: - The method belongs to a class that664 * has @Test(sequential=true) - The parallel attribute is set to "classes" In both these cases,665 * all the methods belonging to that class will then be put in the same worker in order to run in666 * the same thread.667 */668 @Override669 public List<IWorker<ITestNGMethod>> createWorkers(List<ITestNGMethod> methods) {670 AbstractParallelWorker.Arguments args =671 new AbstractParallelWorker.Arguments.Builder()672 .classMethodMap(this.m_classMethodMap)673 .configMethods(this.m_groupMethods)674 .finder(this.m_annotationFinder)675 .invoker(this.m_invoker)676 .methods(methods)677 .testContext(this)678 .listeners(this.m_classListeners.values())679 .build();680 return AbstractParallelWorker.newWorker(m_xmlTest.getParallel()).createWorkers(args);681 }682 //683 // Invoke the workers684 //685 private void runJUnitWorkers(List<? extends IWorker<ITestNGMethod>> workers) {686 //687 // Sequential run688 //689 for (IWorker<ITestNGMethod> tmw : workers) {690 tmw.run();691 }692 }693 private void afterRun() {694 // invoke @AfterTest...
createWorkers
Using AI Code Generation
1import org.testng.TestNG;2import org.testng.TestRunner;3public class TestRunnerTest {4 public static void main(String[] args) {5 TestRunner testRunner = new TestRunner();6 TestNG testNG = new TestNG();7 testNG.setTestClasses(new Class[]{TestNGTest.class});8 testRunner.setTestNG(testNG);9 testRunner.createWorkers(2);10 }11}
createWorkers
Using AI Code Generation
1package com.test;2import org.testng.TestNG;3import org.testng.TestRunner;4public class TestRunnerClass {5 public static void main(String[] args) {6 TestRunner testRunner = new TestRunner();7 testRunner.createWorkers(5);8 TestNG testNG = new TestNG();9 testNG.setTestClasses(new Class[] { TestClass.class });10 testNG.run();11 }12}
createWorkers
Using AI Code Generation
1package com.test;2import org.testng.TestNG;3import org.testng.TestRunner;4public class TestRunnerClass {5public static void main(String[] args) {6TestNG testng = new TestNG();7TestRunner tr = new TestRunner();8tr.createWorkers(2);9testng.setTestClasses(new Class[] { TestClass1.class, TestClass2.class });10testng.run();11}12}
createWorkers
Using AI Code Generation
1public class TestRunner extends TestNG {2 private List<ITestNGMethod> methods = Lists.newArrayList();3 private List<ITestNGMethod> allMethods = Lists.newArrayList();4 private List<ITestNGMethod> allTestMethods = Lists.newArrayList();5 public TestRunner() {6 super();7 }8 public TestRunner(ISuite suite) {9 super(suite);10 }11 public TestRunner(XmlSuite xmlSuite) {12 super(xmlSuite);13 }14 public TestRunner(XmlSuite xmlSuite, ITestObjectFactory objectFactory) {15 super(xmlSuite, objectFactory);16 }17 public void addMethod(ITestNGMethod method) {18 methods.add(method);19 }20 public void addAllMethods(List<ITestNGMethod> methods) {21 allMethods.addAll(methods);22 }23 public void addAllTestMethods(List<ITestNGMethod> methods) {24 allTestMethods.addAll(methods);25 }26 public List<ITestNGMethod> getMethods() {27 return methods;28 }29 public List<ITestNGMethod> getAllMethods() {30 return allMethods;31 }32 public List<ITestNGMethod> getAllTestMethods() {33 return allTestMethods;34 }35 public void run() {36 createWorkers();37 super.run();38 }39}40private void createWorkers() {41 for (int i = 0; i < m_workerThreads; i++) {42 TestWorker worker = new TestWorker(this, i);43 m_workers.add(worker);44 }45}46private void executeTestMethods() {47 for (ITestNGMethod m : m_testMethods) {48 m_workers.get(m_workerIndex++).addMethod(m);49 if (m_workerIndex == m_workers.size()) {50 m_workerIndex = 0;51 }52 }53}54private void runWorkers() {55 for (TestWorker worker : m_workers) {56 worker.start();57 }58}59private void waitForWorkers() {60 for (Test
createWorkers
Using AI Code Generation
1public class TestNGWorkerFactory implements IWorkerFactory {2 private static final Logger logger = Logger.getLogger(TestNGWorkerFactory.class.getName());3 private static final String DEFAULT_TESTNG_XML = "testng.xml";4 private static final String DEFAULT_TESTNG_XML_PATH = "src/test/resources/" + DEFAULT_TESTNG_XML;5 private static final String TESTNG_XML_PATH = "testng.xml.path";6 private TestRunner testRunner;7 public List<ITestNGMethod> createWorkers(List<ITestNGMethod> methods, ITestContext context) {8 List<ITestNGMethod> testNGMethods = new ArrayList<>();9 testRunner = new TestRunner();10 testRunner.setUseDefaultListeners(false);11 testRunner.addListener(new TestNGListener(context));12 String testNGXmlPath = System.getProperty(TESTNG_XML_PATH, DEFAULT_TESTNG_XML_PATH);13 testRunner.setTestSuites(Collections.singletonList(testNGXmlPath));14 try {15 testRunner.createWorkers(methods);16 testNGMethods.addAll(Arrays.asList(testRunner.getTestMethods()));17 } catch (Exception e) {18 logger.log(Level.SEVERE, "Failed to create workers", e);19 }20 return testNGMethods;21 }22 public void finish() {23 testRunner.finish();24 }25}26package com.example.testng;27import org.testng.ITestContext;28import org.testng.ITestListener;29import org.testng.ITestResult;30public class TestNGListener implements ITestListener {31 private ITestContext context;32 public TestNGListener(ITestContext context) {33 this.context = context;34 }35 public void onTestStart(ITestResult result) {36 context.getSuite().getXmlSuite().setParallel(XmlSuite.ParallelMode.METHODS);37 }38 public void onTestSuccess(ITestResult result) {39 }40 public void onTestFailure(ITestResult result) {41 }42 public void onTestSkipped(ITestResult result) {43 }44 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {45 }46 public void onStart(ITestContext context) {47 }48 public void onFinish(ITest
createWorkers
Using AI Code Generation
1import org.testng.TestNG;2import org.testng.xml.XmlSuite;3import java.util.ArrayList;4import java.util.List;5public class TestNGParallelExecution {6 public static void main(String[] args) {7 TestNG testNG = new TestNG();8 List<XmlSuite> suites = new ArrayList<>();9 XmlSuite suite = new XmlSuite();10 suite.setName("TestNG Parallel Execution");11 suite.setParallel(XmlSuite.ParallelMode.METHODS);12 suite.setThreadCount(2);13 suites.add(suite);14 testNG.setXmlSuites(suites);15 testNG.run();16 }17}
createWorkers
Using AI Code Generation
1public class TestRunner {2 private static final Logger LOG = LoggerFactory.getLogger(TestRunner.class);3 private final List<Worker> workers = new ArrayList<Worker>();4 private final ITestContext testContext;5 private final List<ITestNGMethod> testMethods;6 private final ITestListener listener;7 private final TestListenerAdapter tla;8 private final Configuration configuration;9 private final XmlSuite xmlSuite;10 private final IReporter reporter;11 private final List<ISuite> suites;12 private final List<XmlSuite> xmlSuites;13 private final List<XmlTest> xmlTests;14 private final List<XmlClass> xmlClasses;15 private final List<XmlInclude> xmlIncludes;16 private final List<XmlMethodSelector> xmlMethodSelectors;17 private final List<XmlPackage> xmlPackages;18 private final List<XmlParameter> xmlParameters;19 private final List<XmlGroups> xmlGroups;20 private final List<XmlRun> xmlRuns;21 private final List<XmlSuite.ParallelMode> xmlParallelModes;22 private final List<XmlTest.Parameter> xmlTestParameters;23 private final List<XmlTest.Parameter> xmlTestParameter;24 private final List<XmlTest.Parameter> xmlTestParam;25 private final List<XmlTest.Parameter> xmlTestParams;26 private final List<XmlTest.Parameter> xmlTestParamet;27 private final List<XmlTest.Parameter> xmlTestParamete;28 private final List<XmlTest.Parameter> xmlTestParameteres;29 private final List<XmlTest.Parameter> xmlTestParameteres2;30 private final List<XmlTest.Parameter> xmlTestParameteres3;31 private final List<XmlTest.Parameter> xmlTestParameteres4;32 private final List<XmlTest.Parameter> xmlTestParameteres5;33 private final List<XmlTest.Parameter> xmlTestParameteres6;34 private final List<XmlTest.Parameter> xmlTestParameteres7;35 private final List<XmlTest.Parameter> xmlTestParameteres8;
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!!