Reading:  

Working with TestNG - Starters Guide


Execution Procedure of TestNG Annotations

Here we explain the execution procedure of the TestNG methods. There is no format for defining testing methods. The order of the methods called is as shown below in example

Code example

package testNG;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestNGDemo {

	@BeforeClass
	public static void DatabaseConnection() {
		System.out.println("@Before Class");
	}

	@AfterClass
	public static void disConnect() {
		System.out.println("@After Class");
	}

	@BeforeTest
	public void setUp() {
		System.out.println("@BeforeTest");
	}

	@AfterTest
	public void cleanUp() {
		System.out.println("@AfterTest");
	}
	@BeforeSuite
	public void method1() {
		System.out.println("@beforeSuite");
	}

	@AfterSuite
	public void method2() {
		System.out.println("@afterSuite");
	}

	@BeforeMethod
	public void method3() {
		System.out.println("@beforeMethod");
	}

	@AfterMethod
	public void method4() {
		System.out.println("@afterMethod");
	}
	@Test
	public void testExample1() {
		System.out.println("@Test1");
	}
	@Test
	public void testExample2() {
		System.out.println("@Test2");

	}
}


Now in you Eclipse IDE, Right click the TestNGDemo.java file and the Goto Run as > TestNG Test as shown below

Run testNG test

Here is the output we get

@beforeSuite
@BeforeTest
@Before Class
@beforeMethod
@Test1
@afterMethod
@beforeMethod
@Test2
@afterMethod
@After Class
@AfterTest
PASSED: testExample1
PASSED: testExample2

===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================

@afterSuite

===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

The execution procedure is:

  • BeforeSuite() method is executed only once as first of all.
  • AfterSuite() method executes only once at last of all.
  • Methods beforeTest(), beforeClass(), afterClass(), and afterTest() are executed only once.
  • Before executing each test case beforeMethod() method executes.
  • After executing each test case afterMethod() method executes.
  • Each test case executes in between the methods beforeMethod() and afterMethod(),.

 

In next part of this tutorial we will check out how to execute tests

 

Description

This tutorial is focused on getting you started on TestNG, the testing framework inspired from JUnit and NUnit. Here is a quick table of contents

  1. What is TestNG?
  2. Environment Set-up
  3. Writing Tests
  4. Basic Annotations
  5. Execution Procedure
  6. Executing Tests
  7. Suite Test
  8. Ignore Test
  9. Group Test
  10. Exception Test
  11. Dependency Test
  12. Parametrized Test
  13. JUnit Tests
  14. Test Reports
  15. Running tests without Eclipse
  16. Plugin with ANT

 

 



Environment

A computer capable of running Java. IntelliJ Community IDE is also required for this course.

Prerequisites

Good knowledge of Java programming language and eclipse is essential.

Audience

Students looking to get started with TestNG

Learning Objectives

This tutorial will get you started with TestNG.

Author: Subject Coach
Added on: 12th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!