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?


The TestNG Priority is a mechanism used to force a specific execution order among test methods that belong to the same TestNG class. By default, TestNG does not guarantee any particular order for running test methods within a class; they might run in alphabetical order or another unpredictable sequence. Priority allows the developer to override this default behavior.


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

Below is the test class having multiple test methods having attribute priority in @Test annotation.
Output is shown below of the execution of test methods depends on priority values.

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

Below in test class, first three test methods have same priority that 1, last method has priority as 0.
Below is the output shown the execution flow of all test methods. Please have a look.

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.


Suggested Posts:

1. Introduction to TestNG
2. Annotations in TestNG
3. TestNG Asserts
4. First Script in TestNG
5. Parameterization in TestNG

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?


The TestNG Suite centers on its role as the highest organizational container for an entire test execution run. It is the control hub that defines the scope, configuration, and execution strategy for all the individual tests beneath it.


1. The Role as the Top Container 

The TestNG Suite is defined by the root element in the testng.xml configuration file.

  • Logical Grouping: Theoretically, the Suite groups one or more Tests (which are themselves groupings of test classes). It allows you to run a complete, predefined set of tests as a single unit. For instance, an organization might define one Suite for "Daily Smoke Tests" and another for "Full Regression Testing."
  • Single Execution Cycle: A Suite defines a single, complete execution cycle. All setup actions defined at the Suite level (@BeforeSuite) run once at the very beginning, and all cleanup actions (@AfterSuite) run once at the very end, regardless of how many individual tests or classes are executed within it.

2. Configuration and Parameter Control

The Suite provides the highest level of external configuration control.

  • Global Parameters: The Suite level is where you define parameters that are meant to be shared across all the tests, classes, and methods within that Suite. This is ideal for global settings like a base URL, a shared database connection string, or a target environment (e.g., "production" or "staging").

  • Execution Strategy: It dictates how the tests within it will be run. For example, the Suite defines whether the tests should run sequentially (one after the other) or in parallel (concurrently). This is crucial for managing test execution time and efficiency.


3. Reporting Scope

The Suite defines the boundary for the overall reporting mechanism.

  • Consolidated Report: When a Suite completes its run, it generates a single, consolidated report detailing the status (pass, fail, skip) of every Test, every class, and every method executed under its definition. This provides a holistic view of the system's quality status from that specific run.


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

Below is the LoginTest class having two tests annotated with @Test and when Test get executed, it prints a statement on console.

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

Below is the HomePageTest class having two tests annotated with @Test and when Test get executed, it prints a statement on console.

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

Below is Testng.xml file having name of bot Test classes in class tag. each class tag tag is in classes tag which is in test tag. All the tests are in suite tag.

Tags Hierarchy of below Testng.xml file: suite > test > classes > class

<?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


Suggested Posts:

1. Priority in TestNG
2. TestNG Annotations
3. TestNG Asserts
4. Parameterization in TestNG
5. Read Excel Data by Data provider

First TestNG script




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

After installation of TestNG in Eclipse or IntelliJ Idea, we need to follow below steps to write TestNG scripts.


Below are the steps of writing first TestNG script.

a) Create a  java class as test class, we can name as MyFirstTest.java

b) Add TestNG dependencies in POM.xml

c) Write TestNG test code in MyFirstTest.java class as mentioned below.

d) We can directly execute code in eclipse or intellij Idea or create a separate TestNG.xml for Test cases execution, as shown below.

e) Right Click on TestNG.xml file and run the file.














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:

  • Right-click testng.xml
  • Choose Run As → TestNG Suite


Output

Test script executed successfully and logs the statements on console.

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


Suggested Posts:

1. Read Excel Data by Data provider
2. TestNG Dependent Tests
3. TestNG Asserts
4. Parameterization in TestNG
5. Priority in TestNG