Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

TestNG is a testing framework inspired by JUnit and NUnit but introduces some new functionalities, making it more powerful and easier to use. One of the core annotations in TestNG is’ @Test‘, which identifies a method as a test method. Here’s a deep dive into ‘@Test‘ with examples to illustrate its various features and options.

Basic @Test Annotation

The ‘@Test‘ annotation is used to indicate a method as a test method. Here’s a simple example:

				
					import org.testng.annotations.Test;

public class SimpleTest {
    @Test
    public void testMethod() {
        System.out.println("This is a test method.");
    }
}

				
			

TestNG @Test Attributes

The ‘@Test‘ annotation provides several attributes to control the test behavior:

  1. enabled: This attribute is used to enable or disable a test method. By default, it is set to ‘true‘.
				
					@Test(enabled = false)
public void disabledTest() {
    System.out.println("This test will not run.");
}

				
			

2. ‘priority: This attribute is used to define the priority of the test method. Lower priority tests run first

				
					@Test(priority = 1)
public void firstTest() {
    System.out.println("First test.");
}

@Test(priority = 2)
public void secondTest() {
    System.out.println("Second test.");
}

				
			

3. ‘dependsOnMethods: This attribute is used to specify dependencies between test methods. The dependent test method will run only if the specified methods are successful.

				
					@Test
public void login() {
    System.out.println("Login test.");
}

@Test(dependsOnMethods = {"login"})
public void dashboard() {
    System.out.println("Dashboard test.");
}

				
			

4. 'timeOut: This attribute specifies the maximum number of milliseconds a test method should take. If the method exceeds this time, it will be marked as failed.

				
					@Test(timeOut = 1000)
public void timeBoundTest() throws InterruptedException {
    Thread.sleep(500);  // This will pass
}

				
			

5. ‘expectedExceptions: This attribute is used to specify the exceptions that are expected to be thrown by the test method. If the specified exception is thrown, the test is considered successful.

				
					@Test(expectedExceptions = ArithmeticException.class)
public void exceptionTest() {
    int division = 1 / 0;
}

				
			

6. ‘groups: This attribute is used to group multiple test methods. Groups allow for more flexible execution of test methods.

				
					@Test(groups = {"smoke"})
public void smokeTest() {
    System.out.println("Smoke test.");
}

@Test(groups = {"regression"})
public void regressionTest() {
    System.out.println("Regression test.");
}

				
			

Example: Combining Multiple Attributes

Here’s an example that combines several attributes of the @Test annotation:

				
					import org.testng.annotations.Test;

public class CombinedAttributesTest {

    @Test(priority = 1, groups = {"smoke"}, timeOut = 2000)
    public void smokeTest1() {
        System.out.println("Smoke test 1.");
    }

    @Test(priority = 2, groups = {"smoke"}, enabled = false)
    public void smokeTest2() {
        System.out.println("Smoke test 2 - Disabled.");
    }

    @Test(priority = 3, groups = {"regression"}, dependsOnMethods = {"smokeTest1"})
    public void regressionTest() {
        System.out.println("Regression test - Dependent on smokeTest1.");
    }

    @Test(priority = 4, expectedExceptions = NumberFormatException.class)
    public void exceptionTest() {
        Integer.parseInt("Invalid number");
    }
}

				
			

In this example:

  • smokeTest1‘ is a smoke test with a timeout of 2000 milliseconds.
  • smokeTest2‘ is a smoke test but is disabled and will not run.
  • regressionTest‘ is a regression test that depends on the successful execution of ‘smokeTest1‘.
  • exceptionTest‘ expects a ‘NumberFormatException' to be thrown.

Execution Order and Dependencies

The execution order of tests in TestNG is determined by their priority and dependencies. TestNG ensures that all dependencies are respected, and it will execute dependent methods after their dependencies are successfully completed.

				
					import org.testng.annotations.Test;

public class ExecutionOrderTest {

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

    @Test(priority = 2, dependsOnMethods = {"testA"})
    public void testB() {
        System.out.println("Test B - Depends on Test A");
    }

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

				
			

In this case, ‘testB‘ will only run after ‘testA‘ completes successfully, despite the priority values.

The ‘@Test‘ annotation in TestNG is a powerful tool that allows for extensive customization of test methods. By using its various attributes, you can control the execution order, dependencies, expected exceptions, and more, making your tests more robust and easier to manage.

Scroll to Top