- TestNG - TestNG - vs JUnit
- TestNG - Plug with Eclipse
- TestNG - Plug with ANT
- TestNG - Parallel Execution
- TestNG - Asserts
- TestNG - Annotation Transformers
- TestNG - Test Results
- TestNG - Run JUnit Tests
- TestNG - Parameterized Test
- TestNG - Dependency Test
- TestNG - Exception Test
- TestNG - Group Test
- TestNG - Ignore a Test
- TestNG - Suite Test
- TestNG - Executing Tests
- TestNG - Execution Procedure
- TestNG - Basic Annotations
- TestNG - Writing Tests
- TestNG - Environment
- TestNG - Overview
- TestNG - Home
TestNG Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
TestNG - Execution Procedure
This chapter explains the execution procedure of methods in TestNG. It explains the order of the methods called. Here is the execution procedure of the TestNG test API methods with an example.
Create a java class file name TestngAnnotation.java in in /work/testng/src to test annotations.
import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeTest; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeSuite; import org.testng.annotations.AfterSuite; pubpc class TestngAnnotation { // test case 1 @Test pubpc void testCase1() { System.out.println("in test case 1"); } // test case 2 @Test pubpc void testCase2() { System.out.println("in test case 2"); } @BeforeMethod pubpc void beforeMethod() { System.out.println("in beforeMethod"); } @AfterMethod pubpc void afterMethod() { System.out.println("in afterMethod"); } @BeforeClass pubpc void beforeClass() { System.out.println("in beforeClass"); } @AfterClass pubpc void afterClass() { System.out.println("in afterClass"); } @BeforeTest pubpc void beforeTest() { System.out.println("in beforeTest"); } @AfterTest pubpc void afterTest() { System.out.println("in afterTest"); } @BeforeSuite pubpc void beforeSuite() { System.out.println("in beforeSuite"); } @AfterSuite pubpc void afterSuite() { System.out.println("in afterSuite"); } }
Next, let s create the file testng.xml in in /work/testng/src to execute annotations.
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name = "Suite1"> <test name = "test1"> <classes> <class name = "TestngAnnotation"/> </classes> </test> </suite>
Compile the Test case class using javac.
/work/testng/src$ javac TestngAnnotation.java
Now, run the testng.xml, which will run the test case defined in the provided Test Case class.
/work/testng/src$ java org.testng.TestNG testng.xml
Verify the output.
in beforeSuite in beforeTest in beforeClass in beforeMethod in test case 1 in afterMethod in beforeMethod in test case 2 in afterMethod in afterClass in afterTest in afterSuite =============================================== Suite Total tests run: 2, Failures: 0, Skips: 0 ===============================================
Based on the above output, the execution procedure is as follows −
First of all, beforeSuite() method is executed only once.
Lastly, the afterSuite() method executes only once.
Even the methods beforeTest(), beforeClass(), afterClass(), and afterTest() methods are executed only once.
beforeMethod() method executes for each test case but before executing the test case.
afterMethod() method executes for each test case but after executing the test case.
In between beforeMethod() and afterMethod(), each test case executes.