Best Testng code snippet using org.testng.TestRunner.getAllTestMethods
Source:SuiteRunner.java
...140 // (this is used to display the final suite report at the end)141 tr.addListener(m_textReporter);142 m_testRunners.add(tr);143 // Add the methods found in this test to our global count144 m_allTestMethods.addAll(Arrays.asList(tr.getAllTestMethods()));145 }146 }147 @Override148 public XmlSuite getXmlSuite() {149 return m_suite;150 }151 @Override152 public String getName() {153 return m_suite.getName();154 }155 public void setObjectFactory(ITestObjectFactory objectFactory) {156 m_objectFactory = objectFactory;157 }158 public void setReportResults(boolean reportResults) {159 m_useDefaultListeners = reportResults;160 }161 private void invokeListeners(boolean start) {162 for (ISuiteListener sl : m_listeners) {163 if (start) {164 sl.onStart(this);165 }166 else {167 sl.onFinish(this);168 }169 }170 }171 private void setOutputDir(String outputdir) {172 if (isStringBlank(outputdir) && m_useDefaultListeners) {173 outputdir = DEFAULT_OUTPUT_DIR;174 }175 m_outputDir = (null != outputdir) ? new File(outputdir).getAbsolutePath()176 : null;177 }178 private ITestRunnerFactory buildRunnerFactory() {179 ITestRunnerFactory factory = null;180 if (null == m_tmpRunnerFactory) {181 factory = new DefaultTestRunnerFactory(m_configuration,182 m_testListeners.toArray(new ITestListener[m_testListeners.size()]),183 m_useDefaultListeners, m_skipFailedInvocationCounts);184 }185 else {186 factory = new ProxyTestRunnerFactory(187 m_testListeners.toArray(new ITestListener[m_testListeners.size()]),188 m_tmpRunnerFactory);189 }190 return factory;191 }192 @Override193 public String getParallel() {194 return m_suite.getParallel();195 }196 @Override197 public void run() {198 invokeListeners(true /* start */);199 try {200 privateRun();201 }202 finally {203 invokeListeners(false /* stop */);204 }205 }206 private void privateRun() {207 // Map for unicity, Linked for guaranteed order208 Map<Method, ITestNGMethod> beforeSuiteMethods= new LinkedHashMap<Method, ITestNGMethod>();209 Map<Method, ITestNGMethod> afterSuiteMethods = new LinkedHashMap<Method, ITestNGMethod>();210 IInvoker invoker = null;211 // Get the invoker and find all the suite level methods212 for (TestRunner tr: m_testRunners) {213 // TODO: Code smell. Invoker should belong to SuiteRunner, not TestRunner214 // -- cbeust215 invoker = tr.getInvoker();216 for (ITestNGMethod m : tr.getBeforeSuiteMethods()) {217 beforeSuiteMethods.put(m.getMethod(), m);218 }219 for (ITestNGMethod m : tr.getAfterSuiteMethods()) {220 afterSuiteMethods.put(m.getMethod(), m);221 }222 }223 //224 // Invoke beforeSuite methods (the invoker can be null225 // if the suite we are currently running only contains226 // a <file-suite> tag and no real tests)227 //228 if (invoker != null) {229 if(beforeSuiteMethods.values().size() > 0) {230 invoker.invokeConfigurations(null,231 beforeSuiteMethods.values().toArray(new ITestNGMethod[beforeSuiteMethods.size()]),232 m_suite, m_suite.getParameters(), null, /* no parameter values */233 null /* instance */234 );235 }236 Utils.log("SuiteRunner", 3, "Created " + m_testRunners.size() + " TestRunners");237 //238 // Run all the test runners239 //240 boolean testsInParallel = XmlSuite.PARALLEL_TESTS.equals(m_suite.getParallel());241 if (!testsInParallel) {242 runSequentially();243 }244 else {245 runInParallelTestMode();246 }247// SuitePlan sp = new SuitePlan();248// for (TestRunner tr : m_testRunners) {249// sp.addTestPlan(tr.getTestPlan());250// }251// sp.dump();252 //253 // Invoke afterSuite methods254 //255 if (afterSuiteMethods.values().size() > 0) {256 invoker.invokeConfigurations(null,257 afterSuiteMethods.values().toArray(new ITestNGMethod[afterSuiteMethods.size()]),258 m_suite, m_suite.getAllParameters(), null, /* no parameter values */259 null /* instance */);260 }261 }262 }263 private List<IReporter> m_reporters = Lists.newArrayList();264 private void addReporter(IReporter listener) {265 m_reporters.add(listener);266 }267 public List<IReporter> getReporters() {268 return m_reporters;269 }270 private void runSequentially() {271 for (TestRunner tr : m_testRunners) {272 runTest(tr);273 }274 }275 private void runTest(TestRunner tr) {276 tr.run();277 ISuiteResult sr = new SuiteResult(m_suite, tr);278 m_suiteResults.put(tr.getName(), sr);279 }280 /**281 * Implement <suite parallel="tests">.282 * Since this kind of parallelism happens at the suite level, we need a special code path283 * to execute it. All the other parallelism strategies are implemented at the test level284 * in TestRunner#createParallelWorkers (but since this method deals with just one <test>285 * tag, it can't implement <suite parallel="tests">, which is why we're doing it here).286 */287 private void runInParallelTestMode() {288 List<Runnable> tasks= Lists.newArrayList(m_testRunners.size());289 for(TestRunner tr: m_testRunners) {290 tasks.add(new SuiteWorker(tr));291 }292 ThreadUtil.execute(tasks, m_suite.getThreadCount(),293 m_suite.getTimeOut(XmlTest.DEFAULT_TIMEOUT_MS), false);294 }295 private class SuiteWorker implements Runnable {296 private TestRunner m_testRunner;297 public SuiteWorker(TestRunner tr) {298 m_testRunner = tr;299 }300 @Override301 public void run() {302 Utils.log("[SuiteWorker]", 4, "Running XML Test '"303 + m_testRunner.getTest().getName() + "' in Parallel");304 runTest(m_testRunner);305 }306 }307 /**308 * Registers ISuiteListeners interested in reporting the result of the current309 * suite.310 *311 * @param reporter312 */313 protected void addListener(ISuiteListener reporter) {314 m_listeners.add(reporter);315 }316 @Override317 public void addListener(ITestNGListener listener) {318 if (listener instanceof IInvokedMethodListener) {319 m_invokedMethodListeners.add((IInvokedMethodListener) listener);320 }321 if (listener instanceof ISuiteListener) {322 addListener((ISuiteListener) listener);323 }324 if (listener instanceof IReporter) {325 addReporter((IReporter) listener);326 }327 if (listener instanceof IConfigurationListener) {328 m_configuration.addConfigurationListener((IConfigurationListener) listener);329 }330 }331 @Override332 public String getOutputDirectory() {333 return m_outputDir + File.separatorChar + getName();334 }335 @Override336 public Map<String, ISuiteResult> getResults() {337 return m_suiteResults;338 }339 /**340 * FIXME: should be removed?341 *342 * @see org.testng.ISuite#getParameter(java.lang.String)343 */344 @Override345 public String getParameter(String parameterName) {346 return m_suite.getParameter(parameterName);347 }348 /**349 * @see org.testng.ISuite#getMethodsByGroups()350 */351 @Override352 public Map<String, Collection<ITestNGMethod>> getMethodsByGroups() {353 Map<String, Collection<ITestNGMethod>> result = Maps.newHashMap();354 for (TestRunner tr : m_testRunners) {355 ITestNGMethod[] methods = tr.getAllTestMethods();356 for (ITestNGMethod m : methods) {357 String[] groups = m.getGroups();358 for (String groupName : groups) {359 Collection<ITestNGMethod> testMethods = result.get(groupName);360 if (null == testMethods) {361 testMethods = Lists.newArrayList();362 result.put(groupName, testMethods);363 }364 testMethods.add(m);365 }366 }367 }368 return result;369 }...
Source:BaseTest.java
...109 final String desc = test.getDeclaredAnnotation(org.testng.annotations.Test.class).toString();110 Allure.addAttachment("Annotations", desc);111 testResults += "\n///////////////////////////////////////////////////////" + "\n";112 //System.out.println("Total Test Classes: " + ((TestRunner) context).getTestClasses().size());113 testResults += "\nTotal Tests: " + context.getAllTestMethods().length;114 testResults += "\nPassed Tests: " + context.getPassedTests().size();115 testResults += "\nFailed Tests: " + context.getFailedTests().size();116 testResults += "\nSkipped Tests: " + context.getSkippedTests().size();117 testResults += "\nLeft Tests: " + Integer.valueOf(context.getAllTestMethods().length - (context.getPassedTests().size() + context.getFailedTests().size() + context.getSkippedTests().size())).toString() + "\n";118 testResults += "\n///////////////////////////////////////////////////////";119 testResults += "\nTEST CLASS: " + test.getDeclaringClass().getSimpleName() + "\n";120 testResults += "\nTEST: " + testName + "\n";121 testResults += "\nSTATUS: Started: " + "\n";122 testResults += "\nTEST ANNOTATIONS: " + test.getDeclaredAnnotation(org.testng.annotations.Test.class).toString();123 testResults += "\n///////////////////////////////////////////////////////";124 testResults += "\n///////////////////////////////////////////////////////";125 log.info(testResults);126 }127 @AfterMethod(alwaysRun = true, enabled = true)128 public void afterMethod(ITestResult testResult) {129 String testResults = "";130 int SUCCESS = 1;131 int FAILURE = 2;...
Source:TestRunnerFactory.java
...63 init(runner);64 return runner;65 }66 private void init(TestRunner runner){67 convert(runner.getAllTestMethods());68 convert(runner.getAfterSuiteMethods());69 convert(runner.getAfterTestConfigurationMethods());70 convert(runner.getBeforeSuiteMethods());71 convert(runner.getBeforeTestConfigurationMethods());72 }73 private void convert(ITestNGMethod[] m){74 for(int i=0;i<m.length;i++){75 if(m[i] instanceof TestNGMethod && !(m[i] instanceof TestNGScenario)){76 m[i]=new TestNGScenario((TestNGMethod) m[i]);77 }78 }79 }80 81
...
Source:TestRunner.java
...20 protected static TestValueProvider testValueProvider;21 protected WebDriver driver;22 @BeforeSuite(alwaysRun = true)23 public void beforeSuite(ITestContext context) throws IOException {24 for (ITestNGMethod method : context.getAllTestMethods()) {25 method.setRetryAnalyzerClass(Retry.class);26 }27 WebDriverManager.chromedriver().setup();28 testValueProvider = new TestValueProvider();29 }30 @SneakyThrows()31 @BeforeMethod32 public void beforeMethod(ITestContext context) {33 ChromeOptions options = new ChromeOptions();34 if (testValueProvider.getHeadlessMode()) {35 options.addArguments("--headless");36 options.addArguments("--window-size=1920,1080", "--no-sandbox", "'--disable-dev-shm-usage");37 }38 driver = new ChromeDriver(options);...
getAllTestMethods
Using AI Code Generation
1import org.testng.TestNG;2import org.testng.TestRunner;3import org.testng.xml.XmlSuite;4import org.testng.xml.XmlTest;5import java.util.ArrayList;6import java.util.List;7public class TestRunnerTest {8 public static void main(String[] args) {9 TestNG testNG = new TestNG();10 XmlSuite suite = new XmlSuite();11 suite.setName("Suite");12 XmlTest test = new XmlTest(suite);13 test.setName("Test");14 List<String> files = new ArrayList<>();15 files.add("C:\\Users\\saksham\\Desktop\\Selenium\\Selenium\\src\\test\\java\\TestNG\\TestNG.xml");16 test.setSuiteFiles(files);17 testNG.setXmlSuites(new ArrayList<XmlSuite>() {{18 add(suite);19 }});20 testNG.run();21 TestRunner runner = new TestRunner(suite, testNG.getTestListeners());22 List<String> testMethods = runner.getAllTestMethods();23 for (String testMethod : testMethods) {24 System.out.println(testMethod);25 }26 }27}
getAllTestMethods
Using AI Code Generation
1Class<?> testRunnerClass = Class.forName("org.testng.TestRunner");2Method getAllTestMethodsMethod = testRunnerClass.getMethod("getAllTestMethods");3getAllTestMethodsMethod.setAccessible(true);4Object testRunner = testRunnerClass.getConstructor().newInstance();5Object[] allTestMethods = (Object[]) getAllTestMethodsMethod.invoke(testRunner);6for (Object method : allTestMethods) {7 System.out.println(method);8}
getAllTestMethods
Using AI Code Generation
1import java.util.*;2import org.testng.TestRunner;3import org.testng.TestNG;4import org.testng.xml.XmlSuite;5import org.testng.xml.XmlTest;6import org.testng.xml.XmlClass;7import org.testng.xml.XmlInclude;8import org.testng.ITestNGMethod;9import org.testng.Reporter;10public class TestRunnerTest {11 public static void main(String[] args) {12 TestRunner runner = new TestRunner();13 TestNG testng = new TestNG();14 testng.setTestSuites(Arrays.asList("testng.xml"));15 testng.run();16 XmlSuite suite = testng.getXmlSuites().get(0);17 XmlTest test = suite.getTests().get(0);18 XmlClass clazz = test.getXmlClasses().get(0);19 List<XmlInclude> includes = clazz.getIncludedMethods();20 for (XmlInclude include : includes) {21 System.out.println(include.getName());22 }23 }24}
getAllTestMethods
Using AI Code Generation
1import java.util.List;2import java.util.Map;3import java.util.Set;4import org.testng.TestNG;5import org.testng.TestRunner;6import org.testng.xml.XmlSuite;7public class TestNGRunner {8 public static void main(String[] args) {9 TestNG testng = new TestNG();10 testng.setTestSuites(List.of("testng.xml"));11 TestRunner testRunner = testng.createTestRunner();12 List<XmlSuite> suites = testng.getXmlSuites();13 Map<String, Set<String>> tests = testRunner.getAllTestMethods();14 testng.run();15 }16}
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!!