Showing posts with label TestNG Tutorial. Show all posts
Showing posts with label TestNG Tutorial. Show all posts

Priority in TestNG

 




What is priority in TestNG?

In TestNG, the priority attribute is used to define the order of execution of test methods in a class.

By default, TestNG executes test methods in alphabetical order of method names (not the order they appear in the code). If you want to control the execution order explicitly, you can assign a priority to each @Test method.


Key Points about priority:

  • The lower the priority number, the earlier the method will run.

  • priority = 0 will run before priority = 1, and so on.

  • If no priority is specified, TestNG assigns it a default priority of 0.

  • You can assign negative values as well.

  • If two or more tests have the same priority, they run in alphabetical order.



Example Java Code for Priority in TestNG

import org.testng.annotations.Test;

public class PriorityExample {

    @Test(priority = 1)
    public void testC() {
        System.out.println("Test C - Priority 1");
    }

    @Test(priority = 0)
    public void testA() {
        System.out.println("Test A - Priority 0");
    }

    @Test(priority = 2)
    public void testB() {
        System.out.println("Test B - Priority 2");
    }

    @Test  // No priority mentioned, default is 0
    public void testD() {
        System.out.println("Test D - Default Priority (0)");
    }
}




Output:

Test A - Priority 0
Test D - Default Priority (0)
Test C - Priority 1
Test B - Priority 2




Important points:

testA() and testD() both have priority 0. Since testA() comes alphabetically before testD(), it runs first.

Priorities help in defining explicit flow of tests, especially in dependent or staged testing scenarios.

In TestNG, when multiple test methods have the same priority, TestNG does not guarantee any specific execution order among them. It means that the methods with the same priority may be executed in any order, and this order can change across different runs.

Lower priority value = executed earlier

Same priority value = no guaranteed order among those methods



Example Code: Same Priority Test Methods

import org.testng.annotations.Test;

public class SamePriorityExample {

    @Test(priority = 1)
    public void testA() {
        System.out.println("Test A - Priority 1");
    }

    @Test(priority = 1)
    public void testB() {
        System.out.println("Test B - Priority 1");
    }

    @Test(priority = 1)
    public void testC() {
        System.out.println("Test C - Priority 1");
    }

    @Test(priority = 0)
    public void testD() {
        System.out.println("Test D - Priority 0");
    }
}




Output:

Test D - Priority 0
Test A - Priority 1
Test B - Priority 1
Test C - Priority 1




Another Run Might Give:

Test D - Priority 0
Test C - Priority 1
Test A - Priority 1
Test B - Priority 1



If multiple test methods share the same priorityTestNG may run them in any order. So, do not rely on order of execution if you assign the same priority to multiple test methods.

TestNG Suite

Creating a TestNG Test Suite allows you to group and run multiple test classes, groups, or methods together using an XML configuration file. This is very useful for organizing your tests logically and controlling test execution.


What is a TestNG Test Suite?

Test Suite is defined in an XML file generally named as 'testng.xml'. It can:

  • Include multiple test classes

  • Run specific methods or groups

  • Control execution order

  • Pass parameters to tests


Below are the steps to create a TestNG Suite

Step 1: Create Your Test Classes


LoginTest.java

import org.testng.annotations.Test;

public class LoginTest {

    @Test
    public void loginWithValidUser() {
        System.out.println("✔ Login with valid user executed.");
    }

    @Test
    public void loginWithInvalidUser() {
        System.out.println("✔ Login with invalid user executed.");
    }
}



HomePageTest.java

import org.testng.annotations.Test;

public class HomePageTest {

    @Test
    public void checkTitle() {
        System.out.println("✔ Home page title check executed.");
    }

    @Test
    public void checkProfileButton() {
        System.out.println("✔ Profile button check executed.");
    }
}



Step 2: Create the testng.xml Test Suite File

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="MyTestSuite">

    <test name="Login Tests">
        <classes>
            <class name="LoginTest"/>
        </classes>
    </test>

    <test name="Home Page Tests">
        <classes>
            <class name="HomePageTest"/>
        </classes>
    </test>

</suite>


Save testng.xml file in the root of your project (or src/test/resources/ for Maven).

Step 3: Run the Test Suite

In Eclipse or IntelliJ:

  • Right-click on testng.xml

  • Select Run As > TestNG Suite

First TestNG script


This is a simple Testng script which print message before and after tests and run two test methods using Testng annotations.


Step 1: Project Structure

You can create this in EclipseIntelliJ, or any Java IDE.

MyTestNGProject/

├── testng.xml

└── src/

    └── MyFirstTest.java


Step 2: Add TestNG to Project

If using Maven, add this to pom.xml:

<dependencies>
  <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.9.0</version> <!-- Latest version -->
    <scope>test</scope>
  </dependency>
</dependencies>

If not using Maven:



Step 3: Java Code – MyFirstTest.java

import org.testng.annotations.*;

public class MyFirstTest {

    @BeforeClass
    public void setup() {
        System.out.println("Setting up before running any test...");
    }

    @Test
    public void testLogin() {
        System.out.println("Running test: Login functionality");
    }

    @Test
    public void testLogout() {
        System.out.println("Running test: Logout functionality");
    }

    @AfterClass
    public void tearDown() {
        System.out.println("Cleaning up after all tests...");
    }
}



Explanation of Code

@BeforeClassRuns once before any @Test method in this class. Used to initialize resources.

@Test: Marks a method as a test case. TestNG will automatically detect and run it.

@AfterClass: Runs once after all @Test methods have run. Used to clean up resources.



Step 4: Create Testng.xml File

This file tells TestNG which classes to run.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="MyFirstTestSuite">
  <test name="SimpleTest">
    <classes>
      <class name="MyFirstTest"/>
    </classes>
  </test>
</suite>



Step 5: Run the Test

How to Run in Eclipse/IntelliJ:

  1. Right-click testng.xml

  2. Choose Run As → TestNG Suite


Output

Setting up before running any test...
Running test: Login functionality
Running test: Logout functionality
Cleaning up after all tests...