Codes below show the execution order of commonly used annotations of TestNG.
/** * ExecutionOrderOfTestNGAnnotations.java * Show execution order of commonly used annotations of TestNG * Author: Xuan Ngo */ /* // OUTPUT: Run Constructor.(1695 ms) Run @BeforeTest method.(1498 ms) Run @BeforeClass method.(1184 ms) Run @BeforeMethod method.(839 ms) Run @Test method.(621 ms) Run @AfterMethod method.(594 ms) Run @AfterClass method.(1262 ms) Run @AfterTest method.(1971 ms) */ import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterTest; import java.util.Random; public class ExecutionOrderOfTestNGAnnotations { // Variables Random oRandom = new Random(); final int MAX = 2000; // Constructor public ExecutionOrderOfTestNGAnnotations() { final int iRnd = oRandom.nextInt(MAX); this.pause(iRnd); System.out.println("Run Constructor.(" + iRnd + " ms)"); } @BeforeClass public void BeforeClass() { final int iRnd = oRandom.nextInt(MAX); this.pause(iRnd); System.out.println("Run @BeforeClass method.(" + iRnd + " ms)"); } @BeforeTest public void BeforeTest() { final int iRnd = oRandom.nextInt(MAX); this.pause(iRnd); System.out.println("Run @BeforeTest method.(" + iRnd + " ms)"); } @BeforeMethod public void BeforeMethod() { final int iRnd = oRandom.nextInt(MAX); this.pause(iRnd); System.out.println("Run @BeforeMethod method.(" + iRnd + " ms)"); } @Test public void Test() { final int iRnd = oRandom.nextInt(MAX); this.pause(iRnd); System.out.println("Run @Test method.(" + iRnd + " ms)"); } @AfterMethod public void AfterMethod() { final int iRnd = oRandom.nextInt(MAX); this.pause(iRnd); System.out.println("Run @AfterMethod method.(" + iRnd + " ms)"); } @AfterTest public void AfterTest() { final int iRnd = oRandom.nextInt(MAX); this.pause(iRnd); System.out.println("Run @AfterTest method.(" + iRnd + " ms)"); } @AfterClass public void AfterClass() { final int iRnd = oRandom.nextInt(MAX); this.pause(iRnd); System.out.println("Run @AfterClass method.(" + iRnd + " ms)"); } private void pause(long lPauseInMillisSec) { try { Thread.sleep(lPauseInMillisSec); } catch (Exception ex) { System.out.println("Can't sleep."); } } }
http://testng.org/doc/documentation-main.html#annotations
| Attachment | Size |
|---|---|
| ExecutionOrderOfTestNGAnnotations.java | 2.56 KB |