Best Testng code snippet using org.testng.junit.JUnitTestRunner.start
Source:JUnitTestRunner.java
...48 m_parentRunner= notifier;49 }50 51 /**52 * @see junit.framework.TestListener#startTest(junit.framework.Test)53 */54 public void startTest(Test test) {55 m_tests.put(test, new TestRunInfo(Calendar.getInstance().getTimeInMillis())); 56 }57 /**58 * @see junit.framework.TestListener#addError(junit.framework.Test, java.lang.Throwable)59 */60 public void addError(Test test, Throwable t) {61 recordFailure(test, t);62 }63 /**64 * @see junit.framework.TestListener#addFailure(junit.framework.Test, junit.framework.AssertionFailedError)65 */66 public void addFailure(Test test, AssertionFailedError t) {67 recordFailure(test, t);68 }69 private void recordFailure(Test test, Throwable t) {70 TestRunInfo tri= m_tests.get(test);71 if(null != tri) {72 tri.setThrowable(t);73 }74 }75 76 /**77 * @see junit.framework.TestListener#endTest(junit.framework.Test)78 */79 public void endTest(Test test) {80 TestRunInfo tri= m_tests.get(test);81 if(null == tri) {82 return; // HINT: this should never happen. How do I protect myself?83 }84 85 org.testng.internal.TestResult tr= recordResults(test, tri);86 87 runTestListeners(tr, m_parentRunner.getTestListeners());88 }89 private org.testng.internal.TestResult recordResults(Test test, TestRunInfo tri) {90 JUnitUtils.JUnitTestClass tc= new JUnitUtils.JUnitTestClass(test);91 JUnitUtils.JUnitTestMethod tm= new JUnitUtils.JUnitTestMethod(test, tc);92 93 org.testng.internal.TestResult tr= new org.testng.internal.TestResult(tc, 94 test, 95 tm, 96 tri.m_failure, 97 tri.m_start, 98 Calendar.getInstance().getTimeInMillis());99 if(tri.isFailure()) {100 tr.setStatus(ITestResult.FAILURE);101 m_parentRunner.addFailedTest(tm, tr);102 }103 else {104 m_parentRunner.addPassedTest(tm, tr);105 }106 107 m_parentRunner.addInvokedMethod(new InvokedMethod(test, tm, new Object[0], true, false, tri.m_start));108 m_methods.add(tm);109 110 return tr;111 }112 113 private static void runTestListeners(ITestResult tr, List<ITestListener> listeners) {114 for (ITestListener itl : listeners) {115 switch(tr.getStatus()) {116 case ITestResult.SKIP: {117 itl.onTestSkipped(tr);118 break;119 }120 case ITestResult.SUCCESS_PERCENTAGE_FAILURE: {121 itl.onTestFailedButWithinSuccessPercentage(tr);122 break;123 }124 case ITestResult.FAILURE: {125 itl.onTestFailure(tr);126 break;127 }128 case ITestResult.SUCCESS: {129 itl.onTestSuccess(tr);130 break;131 }132 case ITestResult.STARTED: {133 itl.onTestStart(tr);134 break;135 }136 default: {137 assert false : "UNKNOWN STATUS:" + tr;138 }139 }140 }141 }142 143 /**144 * Returns the Test corresponding to the given suite. This is145 * a template method, subclasses override runFailed(), clearStatus().146 */147 protected Test getTest(Class testClass) {148 Method suiteMethod = null;149 try {150 suiteMethod = testClass.getMethod(SUITE_METHODNAME, new Class[0]);151 }152 catch (Exception e) {153 // try to extract a test suite automatically154 return new TestSuite(testClass);155 }156 if (!Modifier.isStatic(suiteMethod.getModifiers())) {157 runFailed(testClass, "suite() method must be static");158 return null;159 }160 Test test = null;161 try {162 test = (Test) suiteMethod.invoke(null, (Object[]) new Class[0]); // static method163 if (test == null) {164 return test;165 }166 }167 catch (InvocationTargetException e) {168 runFailed(testClass, "failed to invoke method suite():" + e.getTargetException().toString());169 return null;170 }171 catch (IllegalAccessException e) {172 runFailed(testClass, "failed to invoke method suite():" + e.toString());173 return null;174 }175 return test;176 }177 /**178 * A <code>start</code> implementation that ignores the <code>TestResult</code>179 * @param testClass the JUnit test class180 */181 public void run(Class testClass) {182 start(testClass);183 }184 185 /**186 * Starts a test run. Analyzes the command line arguments and runs the given187 * test suite.188 */189 public TestResult start(Class testCase) {190 try {191 Test suite = getTest(testCase);192 if(null != suite) {193 return doRun(suite);194 }195 else {196 runFailed(testCase, "could not create/run JUnit test suite");197 }198 }199 catch (Exception e) {200 runFailed(testCase, "could not create/run JUnit test suite: " + e.getMessage());201 }202 203 return null;204 }205 protected void runFailed(Class clazz, String message) {206 throw new TestNGException("Failure in JUnit mode for class " + clazz.getName() + ": " + message);207 }208 /**209 * Creates the TestResult to be used for the test run.210 */211 protected TestResult createTestResult() {212 return new TestResult();213 }214 protected TestResult doRun(Test suite) {215 TestResult result = createTestResult();216 result.addListener(this);217 suite.run(result);218 return result;219 }220 221 private static class TestRunInfo {222 private final long m_start;223 private Throwable m_failure;224 225 public TestRunInfo(long start) {226 m_start= start;227 }228 public boolean isFailure() {229 return null != m_failure;230 }231 232 public void setThrowable(Throwable t) {233 m_failure= t;234 }235 }236}...
start
Using AI Code Generation
1import org.testng.junit.JUnitTestRunner2import org.testng.TestNG3import org.testng.xml.XmlSuite4import org.testng.xml.XmlTest5import org.testng.xml.XmlClass6import org.testng.xml.XmlMethodSelector7import org.testng.xml.XmlMethodSelectors8import org.testng.xml.XmlInclude9import org.testng.xml.XmlGroups10import org.testng.xml.XmlGroup11import org.testng.xml.XmlPackage12def suite = new XmlSuite()13def test = new XmlTest(suite)14def class1 = new XmlClass('testng.TestNGTest1')15def class2 = new XmlClass('testng.TestNGTest2')16def runner = new JUnitTestRunner()17runner.run(new TestNG([suite]))18package testng;19import org.testng.annotations.Test;20public class TestNGTest1 {21 public void testMethod1() {22 System.out.println("TestNGTest1 -> testMethod1");23 }24 public void testMethod2() {25 System.out.println("TestNGTest1 -> testMethod2");26 }27}28package testng;29import org.testng.annotations.Test;30public class TestNGTest2 {31 public void testMethod1() {32 System.out.println("TestNGTest2 -> testMethod1");33 }34 public void testMethod2() {35 System.out.println("TestNGTest2 -> testMethod2");36 }37}
start
Using AI Code Generation
1package org.testng;2import org.testng.junit.JUnitTestRunner;3import org.testng.xml.XmlSuite;4public class TestRunner {5public static void main(String[] args) {6 JUnitTestRunner runner = new JUnitTestRunner();7 runner.start(args);8}9}10package org.testng;11import org.testng.xml.XmlSuite;12public class TestRunner {13public static void main(String[] args) {14 TestNG testNG = new TestNG();15 testNG.setCommandLineSuite(new XmlSuite());16 testNG.run();17}18}19package org.testng;20import org.testng.xml.XmlSuite;21public class TestRunner {22public static void main(String[] args) {23 TestNG testNG = new TestNG();24 testNG.setCommandLineSuite(new XmlSuite());25 testNG.run();26}27}28package org.testng;29import org.testng.xml.XmlSuite;30public class TestRunner {31public static void main(String[] args) {32 TestNG testNG = new TestNG();33 testNG.setCommandLineSuite(new XmlSuite());34 testNG.run();35}36}37package org.testng;38import org.testng.xml.XmlSuite;39public class TestRunner {40public static void main(String[] args) {41 TestNG testNG = new TestNG();42 testNG.setCommandLineSuite(new XmlSuite());43 testNG.run();44}45}46package org.testng;47import org.testng.xml.XmlSuite;48public class TestRunner {49public static void main(String[] args) {50 TestNG testNG = new TestNG();51 testNG.setCommandLineSuite(new XmlSuite());52 testNG.run();53}54}55package org.testng;56import org.testng.xml.XmlSuite;57public class TestRunner {58public static void main(String[] args) {59 TestNG testNG = new TestNG();60 testNG.setCommandLineSuite(new XmlSuite());61 testNG.run();62}63}64package org.testng;65import org.testng.xml.XmlSuite;66public class TestRunner {67public static void main(String[] args) {68 TestNG testNG = new TestNG();69 testNG.setCommandLineSuite(new XmlSuite
start
Using AI Code Generation
1org.testng.junit.JUnitTestRunner runner = new org.testng.junit.JUnitTestRunner()2runner.start(new org.testng.TestNG(), new String[]{})3org.testng.junit.JUnitTestRunner runner = new org.testng.junit.JUnitTestRunner()4runner.start(new org.testng.TestNG(), new String[]{})5org.testng.junit.JUnitTestRunner runner = new org.testng.junit.JUnitTestRunner()6runner.start(new org.testng.TestNG(), new String[]{"testng.xml"})7org.testng.junit.JUnitTestRunner runner = new org.testng.junit.JUnitTestRunner()8runner.start(new org.testng.TestNG(), new String[]{"testng.xml"})9org.testng.junit.JUnitTestRunner runner = new org.testng.junit.JUnitTestRunner()10runner.start(new org.testng.TestNG(), new String[]{"testng.xml"})11org.testng.junit.JUnitTestRunner runner = new org.testng.junit.JUnitTestRunner()12runner.start(new org.testng.TestNG(), new String[]{"testng.xml"})13org.testng.junit.JUnitTestRunner runner = new org.testng.junit.JUnitTestRunner()14runner.start(new org.testng.TestNG(), new String[]{"testng.xml"})15org.testng.junit.JUnitTestRunner runner = new org.testng.junit.JUnitTestRunner()16runner.start(new org.testng.TestNG(), new String[]{"testng.xml"})17org.testng.junit.JUnitTestRunner runner = new org.testng.junit.JUnitTestRunner()18runner.start(new org.testng.TestNG(), new String[]{"testng.xml"})19org.testng.junit.JUnitTestRunner runner = new org.testng.junit.JUnitTestRunner()20runner.start(new org.testng.TestNG(), new String[]{"testng.xml"})21org.testng.junit.JUnitTestRunner runner = new org.testng.junit.JUnitTestRunner()22runner.start(new org.testng.TestNG(), new String[]{"testng.xml"})
start
Using AI Code Generation
1package org.testng.eclipse.ui;2import java.lang.reflect.Method;3import java.util.ArrayList;4import java.util.List;5import org.eclipse.core.resources.IFile;6import org.eclipse.core.resources.IProject;7import org.eclipse.core.resources.IResource;8import org.eclipse.core.resources.IStorage;9import org.eclipse.core.runtime.CoreException;10import org.eclipse.core.runtime.IAdaptable;11import org.eclipse.core.runtime.IPath;12import org.eclipse.core.runtime.IProgressMonitor;13import org.eclipse.core.runtime.Path;14import org.eclipse.core.runtime.Platform;15import org.eclipse.debug.core.ILaunch;16import org.eclipse.debug.core.ILaunchConfiguration;17import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;18import org.eclipse.debug.core.ILaunchManager;19import org.eclipse.debug.core.model.IProcess;20import org.eclipse.debug.core.model.IStreamMonitor;21import org.eclipse.debug.ui.ILaunchShortcut;22import org.eclipse.jdt.core.IJavaElement;23import org.eclipse.jdt.core.IJavaProject;24import org.eclipse.jdt.core.IPackageFragment;25import org.eclipse.jdt.core.IPackageFragmentRoot;26import org.eclipse.jdt.core.IType;27import org.eclipse.jdt.core.JavaCore;28import org.eclipse.jdt.core.JavaModelException;29import org.eclipse.jdt.internal.junit.launcher.JUnitLaunchConfigurationDelegate;30import org.eclipse.jdt.internal.junit.launcher.JUnitMessages;31import org.eclipse.jdt.internal.junit.launcher.JUnitPlugin;32import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;33import org.eclipse.jdt.launching.IJavaProjectLaunchConfigurationConstants;34import org.eclipse.jdt.launching.IRuntimeClasspathEntry;35import org.eclipse.jdt.launching.JavaRuntime;36import org.eclipse.jface.viewers.IStructuredSelection;37import org.eclipse.jface.viewers.StructuredSelection;38import org.eclipse.ui.IEditorPart;39import org.eclipse.ui.IFileEditorInput;40import org.eclipse.ui.IStorageEditorInput;41import org.eclipse.ui.IWorkbenchPage;42import org.eclipse.ui.IWorkbenchPart;43import org.eclipse.ui.PlatformUI;44import org.eclipse.ui.part.FileEditorInput;45import org.eclipse.ui.part.MultiEditorInput;46import org.eclipse.ui.part.MultiEditorInput.EditorInputWrapper;47import org.testng.eclipse.TestNGPlugin;48import org.testng.eclipse.util.IResourceUtils;49import org.testng.eclipse.util.ResourceUtil;50import org.testng.eclipse.util.StringUtils;51import org.testng.eclipse.util.Utils;52import org.testng.eclipse.util.XMLSuiteUtils;53import org.testng.eclipse.util.XMLSuite
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!!