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

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com