Source:How to run test methods in specific order in JUnit4?
if (expression)
{
// Run 1
} else {
// Run 2
}
Best Testng code snippet using org.testng.TestNG
Source:TestNGMethodStub.java
...17import java.util.concurrent.Callable;18import org.testng.IClass;19import org.testng.IRetryAnalyzer;20import org.testng.ITestClass;21import org.testng.ITestNGMethod;22import org.testng.internal.ConstructorOrMethod;23import org.testng.xml.XmlTest;24/**25 * Understands an <code>{@link ITestNGMethod}</code> stub for testing purposes.26 *27 * @author Alex Ruiz28 */29public class TestNGMethodStub implements ITestNGMethod {30 private static final long serialVersionUID = 1L;31 private String methodName;32 private Method method;33 /** @see org.testng.ITestNGMethod#addMethodDependedUpon(java.lang.String) */34 @Override35 public void addMethodDependedUpon(String newMethodName) {36 }37 /** @see org.testng.ITestNGMethod#canRunFromClass(org.testng.IClass) */38 @Override39 public boolean canRunFromClass(IClass testClass) {40 return false;41 }42 /** @see org.testng.ITestNGMethod#getAfterGroups() */43 @Override44 public String[] getAfterGroups() {45 return null;46 }47 /** @see org.testng.ITestNGMethod#getBeforeGroups() */48 @Override49 public String[] getBeforeGroups() {50 return null;51 }52 /** @see org.testng.ITestNGMethod#getCurrentInvocationCount() */53 @Override54 public int getCurrentInvocationCount() {55 return 0;56 }57 /** @see org.testng.ITestNGMethod#getDate() */58 @Override59 public long getDate() {60 return 0;61 }62 /** @see org.testng.ITestNGMethod#getDescription() */63 @Override64 public String getDescription() {65 return null;66 }67 /** @see org.testng.ITestNGMethod#getGroups() */68 @Override69 public String[] getGroups() {70 return null;71 }72 /** @see org.testng.ITestNGMethod#getGroupsDependedUpon() */73 @Override74 public String[] getGroupsDependedUpon() {75 return null;76 }77 /** @see org.testng.ITestNGMethod#getId() */78 @Override79 public String getId() {80 return null;81 }82 /** @see org.testng.ITestNGMethod#getInstanceHashCodes() */83 @Override84 public long[] getInstanceHashCodes() {85 return null;86 }87 /** @see org.testng.ITestNGMethod#getInstances() */88 @Override89 public Object[] getInstances() {90 return null;91 }92 /** @see org.testng.ITestNGMethod#getInvocationCount() */93 @Override94 public int getInvocationCount() {95 return 0;96 }97 /** @see org.testng.ITestNGMethod#getMethod() */98 @Override99 public Method getMethod() {100 return method;101 }102 public void setMethod(Method method) {103 this.method = method;104 }105 /** @see org.testng.ITestNGMethod#getMethodName() */106 @Override107 public String getMethodName() {108 return methodName;109 }110 public void setMethodName(String methodName) {111 this.methodName = methodName;112 }113 /** @see org.testng.ITestNGMethod#getMethodsDependedUpon() */114 @Override115 public String[] getMethodsDependedUpon() {116 return null;117 }118 /** @see org.testng.ITestNGMethod#getMissingGroup() */119 @Override120 public String getMissingGroup() {121 return null;122 }123 /** @see org.testng.ITestNGMethod#getParameterInvocationCount() */124 @Override125 public int getParameterInvocationCount() {126 return 0;127 }128 /** @see org.testng.ITestNGMethod#getRealClass() */129 @Override130 public Class<?> getRealClass() {131 return null;132 }133 /** @see org.testng.ITestNGMethod#getSuccessPercentage() */134 @Override135 public int getSuccessPercentage() {136 return 0;137 }138 /** @see org.testng.ITestNGMethod#getTestClass() */139 @Override140 public ITestClass getTestClass() {141 return null;142 }143 /** @see org.testng.ITestNGMethod#getThreadPoolSize() */144 @Override145 public int getThreadPoolSize() {146 return 0;147 }148 /** @see org.testng.ITestNGMethod#getTimeOut() */149 @Override150 public long getTimeOut() {151 return 0;152 }153 /** @see org.testng.ITestNGMethod#incrementCurrentInvocationCount() */154 @Override155 public void incrementCurrentInvocationCount() {156 }157 /** @see org.testng.ITestNGMethod#isAfterClassConfiguration() */158 @Override159 public boolean isAfterClassConfiguration() {160 return false;161 }162 /** @see org.testng.ITestNGMethod#isAfterGroupsConfiguration() */163 @Override164 public boolean isAfterGroupsConfiguration() {165 return false;166 }167 /** @see org.testng.ITestNGMethod#isAfterMethodConfiguration() */168 @Override169 public boolean isAfterMethodConfiguration() {170 return false;171 }172 /** @see org.testng.ITestNGMethod#isAfterSuiteConfiguration() */173 @Override174 public boolean isAfterSuiteConfiguration() {175 return false;176 }177 /** @see org.testng.ITestNGMethod#isAfterTestConfiguration() */178 @Override179 public boolean isAfterTestConfiguration() {180 return false;181 }182 /** @see org.testng.ITestNGMethod#isAlwaysRun() */183 @Override184 public boolean isAlwaysRun() {185 return false;186 }187 /** @see org.testng.ITestNGMethod#isBeforeClassConfiguration() */188 @Override189 public boolean isBeforeClassConfiguration() {190 return false;191 }192 /** @see org.testng.ITestNGMethod#isBeforeGroupsConfiguration() */193 @Override194 public boolean isBeforeGroupsConfiguration() {195 return false;196 }197 /** @see org.testng.ITestNGMethod#isBeforeMethodConfiguration() */198 @Override199 public boolean isBeforeMethodConfiguration() {200 return false;201 }202 /** @see org.testng.ITestNGMethod#isBeforeSuiteConfiguration() */203 @Override204 public boolean isBeforeSuiteConfiguration() {205 return false;206 }207 /** @see org.testng.ITestNGMethod#isBeforeTestConfiguration() */208 @Override209 public boolean isBeforeTestConfiguration() {210 return false;211 }212 /** @see org.testng.ITestNGMethod#isTest() */213 @Override214 public boolean isTest() {215 return false;216 }217 /** @see org.testng.ITestNGMethod#setDate(long) */218 @Override219 public void setDate(long date) {220 }221 /** @see org.testng.ITestNGMethod#setId(java.lang.String) */222 @Override223 public void setId(String arg0) {224 }225 /** @see org.testng.ITestNGMethod#setInvocationCount(int) */226 @Override227 public void setInvocationCount(int arg0) {228 }229 /** @see org.testng.ITestNGMethod#setMissingGroup(java.lang.String) */230 @Override231 public void setMissingGroup(String group) {232 }233 /** @see org.testng.ITestNGMethod#setParameterInvocationCount(int) */234 @Override235 public void setParameterInvocationCount(int arg0) {236 }237 /** @see org.testng.ITestNGMethod#setTestClass(org.testng.ITestClass) */238 @Override239 public void setTestClass(ITestClass cls) {240 }241 /** @see org.testng.ITestNGMethod#setThreadPoolSize(int) */242 @Override243 public void setThreadPoolSize(int arg0) {244 }245 @Override246 public ITestNGMethod clone() {247 return null;248 }249 /** @see org.testng.ITestNGMethod#getRetryAnalyzer() */250 @Override251 public IRetryAnalyzer getRetryAnalyzer() {252 return null;253 }254 /** @see org.testng.ITestNGMethod#setRetryAnalyzer(org.testng.IRetryAnalyzer) */255 @Override256 public void setRetryAnalyzer(IRetryAnalyzer arg0) {257 }258 /** @see org.testng.ITestNGMethod#setSkipFailedInvocations(boolean) */259 @Override260 public void setSkipFailedInvocations(boolean arg0) {261 }262 /** @see org.testng.ITestNGMethod#skipFailedInvocations() */263 @Override264 public boolean skipFailedInvocations() {265 return false;266 }267 @Override268 public Object getInstance() {269 // TODO Auto-generated method stub270 return null;271 }272 @Override273 public void setTimeOut(long timeOut) {274 // TODO Auto-generated method stub275 }276 @Override...
Source:TestContextStub.java
...18import org.testng.IClass;19import org.testng.IResultMap;20import org.testng.ISuite;21import org.testng.ITestContext;22import org.testng.ITestNGMethod;23import org.testng.xml.XmlTest;24import com.google.inject.Injector;25import com.google.inject.Module;26/**27 * Understands an <code>{@link ITestContext}</code> stub for testing purposes.28 *29 * @author Alex Ruiz30 */31public class TestContextStub implements ITestContext {32 private static final long serialVersionUID = 1L;33 private String outputDirectory;34 /** @see org.testng.ITestContext#getAllTestMethods() */35 @Override36 public ITestNGMethod[] getAllTestMethods() {37 return null;38 }39 /** @see org.testng.ITestContext#getEndDate() */40 @Override41 public Date getEndDate() {42 return null;43 }44 /** @see org.testng.ITestContext#getExcludedGroups() */45 @Override46 public String[] getExcludedGroups() {47 return null;48 }49 /** @see org.testng.ITestContext#getExcludedMethods() */50 @Override51 public Collection<ITestNGMethod> getExcludedMethods() {52 return null;53 }54 /** @see org.testng.ITestContext#getFailedButWithinSuccessPercentageTests() */55 @Override56 public IResultMap getFailedButWithinSuccessPercentageTests() {57 return null;58 }59 /** @see org.testng.ITestContext#getFailedConfigurations() */60 @Override61 public IResultMap getFailedConfigurations() {62 return null;63 }64 /** @see org.testng.ITestContext#getFailedTests() */65 @Override...
Source:TestNGActiTimeHeader.java
...3637383940public class TestNGActiTimeHeader extends Base {41 42 private int testId;43 WebDriver driver;44 ActiTimeHeader actiTimeHeader;45 LoginPage loginPage;46 SoftAssert softAssert;47 static ExtentTest test;48 static ExtentHtmlReporter reporter;49 50 @BeforeTest51 @Parameters("browser")52 public void launchBrowser(String browser) {53 reporter = new ExtentHtmlReporter("test-output"+File.separator+"ExtendReport"+File.separator+"extendReport.html");54 ExtentReports extend = new ExtentReports();
...
Source:MultiBrowserTest.java
1package multibrowser;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.firefox.FirefoxDriver;6import org.openqa.selenium.ie.InternetExplorerDriver;7import org.testng.Assert;8import org.testng.annotations.AfterClass;9import org.testng.annotations.AfterMethod;10import org.testng.annotations.AfterTest;11import org.testng.annotations.BeforeClass;12import org.testng.annotations.BeforeMethod;13import org.testng.annotations.BeforeTest;14import org.testng.annotations.Listeners;15import org.testng.annotations.Parameters;16import org.testng.annotations.Test;17import org.testng.util.RetryAnalyzerCount;18import java.util.concurrent.TimeUnit;19public class MultiBrowserTest 20 21{ 22 public WebDriver driver;23 // Configure for multi browser drivers24 //@Parameters("browser")25 26 @BeforeTest27 public void beforeTest() {28 driver = new FirefoxDriver();29 30 System.out.println("In Beforetest");31 } 32 33 @Test(priority = 1) 34 public void googleLaunch() throws InterruptedException{35 //System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\chromedriver.exe");36 37 System.out.println("entering into test cases_GOOGLE");38 driver.get("http://google.com");39 System.out.println("google.com opened");40 41 42 Thread.sleep(2000);43 driver.findElement(By.id("gbqfq")).sendKeys("testing"); 44 //driver.quit();45 driver.findElement(By.id("gbqfb")).click();46 47 }48 49 @Test(priority = 2) 50 public void assertion2(){51 System.out.println("In failed test2");52 Assert.fail("Assert.fail2");53 54 }55 56 @Test(priority = 3) 57 public void assertion3(){58 System.out.println("In failed test3");59 Assert.fail("Assert.fail3");60 61 }62 @AfterTest63 public void aftertest() {64 65 System.out.println("In AfterTest1");66 driver.quit();67 System.out.println("In AfterTest2");68 69 }70}...
Source:TestngAnnotation.java
1package JavaBasics.Testng;2import org.testng.annotations.Test;3import org.testng.annotations.BeforeMethod;4import org.testng.annotations.AfterMethod;5import org.testng.annotations.BeforeClass;6import org.testng.annotations.AfterClass;7import org.testng.annotations.BeforeTest;8import org.testng.annotations.AfterTest;9import org.testng.annotations.BeforeSuite;10import org.testng.annotations.AfterSuite;11import org.testng.annotations.Test;12import org.testng.annotations.BeforeMethod;13import org.testng.annotations.AfterMethod;14import org.testng.annotations.BeforeClass;15import org.testng.annotations.AfterClass;16import org.testng.annotations.BeforeTest;17import org.testng.annotations.AfterTest;18import org.testng.annotations.BeforeSuite;19import org.testng.annotations.AfterSuite;20public class TestngAnnotation {21 // test case 122 @Test23 public void testCase1() {24 System.out.println("in test case 1");25 }26 // test case 227 @Test28 public void testCase2() {29 System.out.println("in test case 2");30 }31 @BeforeMethod32 public void beforeMethod() {33 System.out.println("in beforeMethod");34 }35 @AfterMethod36 public void afterMethod() {37 System.out.println("in afterMethod");38 }39 @BeforeClass40 public void beforeClass() {41 System.out.println("in beforeClass");42 }43 @AfterClass44 public void afterClass() {45 System.out.println("in afterClass");46 }47 @BeforeTest48 public void beforeTest() {49 System.out.println("in beforeTest");50 }51 @AfterTest52 public void afterTest() {53 System.out.println("in afterTest");54 }55 @BeforeSuite56 public void beforeSuite() {57 System.out.println("in beforeSuite");58 }59 @AfterSuite60 public void afterSuite() {61 System.out.println("in afterSuite");62 }63}...
Source:NewTest.java
1package test;2import org.testng.annotations.Test;3import org.testng.annotations.BeforeMethod;4import org.testng.annotations.AfterMethod;5import org.testng.annotations.DataProvider;6import org.testng.annotations.BeforeClass;7import java.security.acl.Group;8import org.testng.Assert;9import org.testng.IRetryAnalyzer;10import org.testng.ITestResult;11import org.testng.annotations.AfterClass;12import org.testng.annotations.BeforeTest;13import org.testng.annotations.AfterTest;14import org.testng.annotations.BeforeSuite;15import org.testng.annotations.AfterSuite;16public class NewTest{17@Test (retryAnalyzer=test.ReTryAnilizer.class)18 public void test1 (){19 Assert.assertEquals(true, false);20 System.out.println("Test1");21 }22 @Test (groups="smoke")23 public void test2 (){24 System.out.println("Test2");25 }26 @Test (groups="regression")27 public void test3 (){28 System.out.println("Test3");29 }30 31 @BeforeMethod32 public void beforeMethod() {33 System.out.println("Before method");34 }35 @AfterMethod36 public void afterMethod() {37 System.out.println("ater method");38 }39// @DataProvider40// public Object[][] dp() {41// return new Object[][] {42// new Object[] { 1, "a" },43// new Object[] { 2, "b" },44// };45// }46 @BeforeClass47 public void beforeClass() {48 System.out.println("Before Class");49 }50 @AfterClass51 public void afterClass() {52 System.out.println("After class");53 }54 @BeforeTest55 public void beforeTest() {56 System.out.println("Before test");57 }58 @AfterTest59 public void afterTest() {60 System.out.println("After test");61 }62 @BeforeSuite63 public void beforeSuite() {64 System.out.println("Before suite");65 }66 @AfterSuite67 public void afterSuite() {68 System.out.println("After suite");69 }70}...
Source:TeastNgAnnotations.java
1package testNgTestCases;23import org.testng.annotations.AfterClass;4import org.testng.annotations.AfterMethod;5import org.testng.annotations.AfterSuite;6import org.testng.annotations.AfterTest;7import org.testng.annotations.BeforeClass;8import org.testng.annotations.BeforeMethod;9import org.testng.annotations.BeforeSuite;10import org.testng.annotations.BeforeTest;11import org.testng.annotations.Test;1213public class TeastNgAnnotations {14 @BeforeSuite15 public void beforeSuite() {16 System.out.println("BeforeSuite");17 }18 @BeforeTest19 public void beforeTest() {20 System.out.println("BeforeTest");21 }22 @BeforeClass23 public void beforeClass() {24 System.out.println("BeforeClass"); 25 }26 @BeforeMethod27public void beforeMethod() {28 System.out.println("BeforeMethod");29 }30 @Test(priority=0)31 public void test0() {32 System.out.println("Test0");33 }34 @Test(priority=1)35 private void test1() {36 System.out.println("Test1");37 }38 @Test(priority=2)39 public void test2() {40 System.out.println("Test2");41 }42 @AfterMethod43 public void afterMethod() {44 System.out.println("AfterMethod");45 }46 @AfterClass47 public void afterClass() {48 System.out.println("AfterClass");49 }50 @AfterTest51 public void afterTest() {52 System.out.println("AfterTest");53 }54 @AfterSuite55 public void afterSuite() {56 System.out.println("AfterSuite");57 }58 59}
...
Source:Test1inTestNg1.java
1package myTest1;2import org.testng.annotations.AfterMethod;3import org.testng.annotations.Test;4import org.testng.annotations.BeforeMethod;5import org.testng.AssertJUnit;6import org.testng.annotations.Test;7import io.github.bonigarcia.wdm.WebDriverManager;8import org.testng.annotations.BeforeMethod;9import java.util.concurrent.TimeUnit;10import org.openqa.selenium.WebDriver;11import org.openqa.selenium.firefox.FirefoxDriver;12import org.testng.Assert;13import org.testng.annotations.AfterMethod;14public class Test1inTestNg1 {15 WebDriver driver;16 @BeforeMethod17 public void setUp() throws Exception {18 19 WebDriverManager.firefoxdriver().setup();20 driver = new FirefoxDriver();21 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);22 23 }24 25 @Test26 public void test1() throws Exception {27 driver.navigate().to("http://google.com");28 System.out.println("Test2 "+driver.getTitle());29 AssertJUnit.assertEquals(driver.getTitle(), "Google");30 }31 @AfterMethod32 public void tearDown() throws Exception {33 driver.manage().deleteAllCookies();34 Thread.sleep(2000);35 driver.quit();36 System.out.println("Exittttttttttttt");37 }38}...
TestNG
Using AI Code Generation
1import org.testng.annotations.Test;2import org.testng.Assert;3import org.testng.annotations.DataProvider;4import org.testng.annotations.BeforeClass;5import org.testng.annotations.AfterClass;6public class TestNG_DataProvider {7 @Test(dataProvider = "dp")8 public void f(Integer n, String s) {9 Assert.assertEquals(n, Integer.valueOf(s));10 }11 public Object[][] dp() {12 return new Object[][] {13 new Object[] { 1, "1" },14 new Object[] { 2, "2" },15 new Object[] { 3, "3" },16 };17 }18}
TestNG
Using AI Code Generation
1import org.testng.annotations.Test;2public class TestNGClass {3 public void test() {4 System.out.println("TestNG Test");5 }6}7import org.junit.Test;8public class JUnitClass {9 public void test() {10 System.out.println("JUnit Test");11 }12}13import org.junit.Test;14public class JUnit4Class {15 public void test() {16 System.out.println("JUnit4 Test");17 }18}19import org.junit.jupiter.api.Test;20public class JUnit5Class {21 public void test() {22 System.out.println("JUnit5 Test");23 }24}25import org.junit.jupiter.api.Test;26public class JUnit5Class {27 public void test() {28 System.out.println("JUnit5 Test");29 }30}31import org.junit.jupiter.api.Test;32public class JUnit5Class {33 public void test() {34 System.out.println("JUnit5 Test");35 }36}37import org.junit.jupiter.api.Test;38public class JUnit5Class {39 public void test() {40 System.out.println("JUnit5 Test");41 }42}43import org.junit.jupiter.api.Test;44public class JUnit5Class {45 public void test() {46 System.out.println("JUnit5 Test");47 }48}49import org.junit.jupiter.api.Test;50public class JUnit5Class {51 public void test() {52 System.out.println("JUnit5 Test");53 }54}55import org.testng.annotations.Test;56public class TestNGClass {57 public void test() {58 System.out.println("TestNG Test");59 }60}61import org.junit.Test;62public class JUnitClass {63 public void test() {64 System.out.println("JUnit Test");65 }66}
TestNG
Using AI Code Generation
1import org.testng.annotations.Test;2public class TestNGDemo {3public void testMethod() {4System.out.println("This is test method");5}6}7[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TestNG ---
1if (expression)2{3 // Run 14} else {5 // Run 26}7
1MOV R0, #0 // R0 = sum = 02MOV R1, #0 // R1 = c = 03ADR R2, data // R2 = addr of data array (put this instruction outside outer loop)4.inner_loop // Inner loop branch label5 LDRB R3, [R2, R1] // R3 = data[c]6 CMP R3, #128 // compare R3 to 1287 ADDGE R0, R0, R3 // if R3 >= 128, then sum += data[c] -- no branch needed!8 ADD R1, R1, #1 // c++9 CMP R1, #arraySize // compare c to arraySize10 BLT inner_loop // Branch to inner_loop if c < arraySize11
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!!