TestNG – 3 || Create and execute multiple Test Cases.

As we know that TestNG is a framework. We can create and execute multiple cases in a single class file.

In the last chapter, we have learned how to define and execute a simple program using TestNG Annotation. The same way, we can use annotation @Test to declare multiple test cases in a single program.

Create and Execute Multiple Test Cases
Create and Execute Multiple Test Cases

Create and Execute Multiple Test Cases

We can define the flow of execution in many ways while writing the code. But all of the above, we have two basic and easy Rules.

  1. Alphabetical Order based on Method names.
  2. Priority Order based on priority attribute for each method.

Alphabetical Order

This is also called the default execution order of the methods for a program. Because, in this process, TestNG automatically decides the sequence of the methods for execution, based on the Method names.

In the above image, we observe that there are three methods with sequence MethodA(), MethodC(), MethodB(). See below code for more clarity.

import org.testng.annotations.Test;
/**
 * @author ashok.kumar
 *
 */
public class DefaultFlow 
{
	@Test
	public void MethodA()
	{
		System.out.println("Method A Called.");
	}
	
	@Test
	public void MethodC()
	{
		System.out.println("Method C Called.");
	}
	
	@Test
	public void MethodB()
	{
		System.out.println("Method B Called.");
	}
}
  • Output: Execution in alphabetical order
    • Method A Called.
    • Method B Called.
    • Method C Called.
    • PASSED: MethodA
    • PASSED: MethodB
    • PASSED: MethodC

Priority Order

This is a manual process of defining the order of methods during execution irrespective below points:

  1. No matter in which sequence Methods have been defined.
  2. No matter the name of the methods as in Alphabetical Order.

In other words, we need to just define priority for individual methods while writing the code.

Priority is an attribute in key=value format. Where value should be a numeric value.

Note: If there are two methods having the same priority value than it follows Alphabetical Order rule.

Please refer below code example to know how to set priority for individual methods.

import org.testng.annotations.Test;

/**
 * @author ashok.kumar
 *
 */
public class PriorityFlow 
{
	@Test(priority=3)
	public void MethodA()
	{
		System.out.println("Method A Called.");
	}
	
	@Test(priority=1)
	public void MethodC()
	{
		System.out.println("Method C Called.");
	}
	
	@Test(priority=2)
	public void MethodB()
	{
		System.out.println("Method B Called.");
	}
}
  • Output: Execution in Priority Order
    • Method C Called.
    • Method B Called.
    • Method A Called.
    • PASSED: MethodC
    • PASSED: MethodB
    • PASSED: MethodA

Related Links:

Java Basics:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

Excel:


216total visits,1visits today

Leave a Reply

Your email address will not be published. Required fields are marked *