TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

TestNG

TestNG Test Priority

Overview

In this post, we will learn some code examples about TestNG priority and how to set to @Test methods.By default TestNG executes the tests in order of the names and not in the order you write test methods in a class. To demonstrate it we will see code examples.

Code Example 1

package com.testingdocs.testng.sample;

import org.testng.annotations.Test;

public class TestNGPriorityExample 
{
 @Test 
 public void aMethod(){ 
 System.out.println("TestingDocs >> aMethod"); 
 } 
 
 @Test 
 public void bMethod(){ 
 System.out.println("TestingDocs >> bMethod"); 
 } 
 
 @Test 
 public void cMethod(){ 
 System.out.println("TestingDocs >> cMethod"); 
 }

}

 

PASSED: aMethod
PASSED: bMethod
PASSED: cMethod

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

Code Example 2

In this example we will reverse the order of methods written in the class and execute it. You can notice that there is no change in the TestNG default order of execution in both the examples.

package com.testingdocs.testng.sample;

import org.testng.annotations.Test;

public class TestNGPriorityExample 
{
 @Test 
 public void cMethod(){ 
 System.out.println("TestingDocs >> cMethod"); 
 }
 
 @Test 
 public void bMethod(){ 
 System.out.println("TestingDocs >> bMethod"); 
 } 
 
 @Test 
 public void aMethod(){ 
 System.out.println("TestingDocs >> aMethod"); 
 } 
 
}

 

PASSED: aMethod
PASSED: bMethod
PASSED: cMethod

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

TestNG_Priority1

 

Now we want the methods to execute in a particular orderĀ  lets say : c —-> b ——–> a

TestNG framework provides an attribute to @Test annotation called priority. Priority controls the run scheduling order of the test method. Lower priorities will be scheduled first.

We will achieve the order of execution in the below code example. Notice how we set priority to the test methods.We will set cMethod lower priority so that the methods executes first. bMethod executes next with priority 2 and then aMethod with priority set to 3.

Code Example 3

package com.testingdocs.testng.sample;

import org.testng.annotations.Test;

public class TestNGPriorityExample 
{
 @Test(priority=3) 
 public void aMethod(){ 
 System.out.println("TestingDocs >> aMethod"); 
 } 
 
 @Test(priority=2) 
 public void bMethod(){ 
 System.out.println("TestingDocs >> bMethod"); 
 } 
 
 @Test(priority=1) 
 public void cMethod(){ 
 System.out.println("TestingDocs >> cMethod"); 
 }
 
}

 

PASSED: cMethod
PASSED: bMethod
PASSED: aMethod

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

 

TestNG_Priority2

 

Now we will see one exception for the order of execution using the priority of the test methods. In the next example, we set method dependency and force to the reverse order of execution using priorities to test methods.

In the below example method2 depends on method1. So even if method2 is set lower priority to execute first, since it is dependent on method1, method1 we will executed first.

Code Example 4

public class TestNGPriorityExample2
{
 
 @Test(priority=2) 
 public void method1(){ 
 System.out.println("TestingDocs >> In method1"); 
 Assert.assertEquals("PASS", "PASS"); 
 } 
 
 
 @Test(priority=1,dependsOnMethods={"method1"}) 
 public void method2(){ 
 System.out.println("TestingDocs >> method2"); 
 } 
 

}

 

PASSED: method1
PASSED: method2

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

 

TestNG Tutorials on this website can be found at:

https://www.testingdocs.com/testng-framework-tutorial/

For more details on the TestNG Framework, visit the official website of TestNG at:

https://testng.org

Related Posts

Tests TestNG Suite testng.xml

TestNG /

Run tests from TestNG Suite testng.xml file

Add TestNG library

TestNG /

Add the TestNG library to the project

new TestNG class

TestNG /

Create TestNG test class in Eclipse IDE.

TestNG Plugin IntelliJ

TestNG /

Enable TestNG in IntelliJ IDE

Installing Eclipse from Update Site

TestNG /

Install TestNG latest version from the update site

‹ TestNG Multiple Exception Tests› Custom Assertions in TestNG Framework

Recent Posts

  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • RAPTOR Editions
  • Flowgorithm Conditional Breakpoint Statement
  • Flowgorithm Read Numbers from File Example
  • Search Text File Flowchart Example
  • Flowgorithm Turtle Graphics Symbols
  • Draw Circle using Flowgorithm Turtle
  • Draw Parallel Lines using Flowgorithm Graphics

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version