Skip to toolbar

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:


Leave a Reply

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

Site Statistics

  • Users online: 0 
  • Visitors today : 1
  • Page views today : 1
  • Total visitors : 49,375
  • Total page view: 67,972

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.