0% found this document useful (0 votes)
6 views5 pages

TestNG Annotations Exercies

The document provides an overview of basic and advanced TestNG annotations used for testing in Java. It covers annotations for marking test methods, setting up and tearing down tests, and managing execution order and dependencies between tests. Examples are included to illustrate the usage of each annotation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

TestNG Annotations Exercies

The document provides an overview of basic and advanced TestNG annotations used for testing in Java. It covers annotations for marking test methods, setting up and tearing down tests, and managing execution order and dependencies between tests. Examples are included to illustrate the usage of each annotation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Basic TestNG Annotations

🔥 @Test – Marks a method as a test case.


import org.testng.annotations.Test;

public class BasicTest {

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

📝 Output:
This is a simple test method.

🔄 2. Setup and Teardown Annotations


These annotations help set up prerequisites and clean up after tests.
⚡ @BeforeMethod & @AfterMethod
 Runs before and after each test method.

import org.testng.annotations.*;

public class MethodLevelTest {

@BeforeMethod
public void setUp() {
System.out.println("Setting up before each test.");
}

@Test
public void testOne() {
System.out.println("Executing Test One.");
}

@Test
public void testTwo() {
System.out.println("Executing Test Two.");
}

@AfterMethod
public void tearDown() {
System.out.println("Cleaning up after each test.\n");
}
}
📝 Output:
Setting up before each test.
Executing Test One.
Cleaning up after each test.

Setting up before each test.


Executing Test Two.
Cleaning up after each test.

🌐 @BeforeClass & @AfterClass


 Runs once before and after all methods in the class.

import org.testng.annotations.*;

public class ClassLevelTest {

@BeforeClass
public void beforeClass() {
System.out.println("Runs once before all tests in this class.");
}

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

@Test
public void testB() {
System.out.println("Running Test B.");
}

@AfterClass
public void afterClass() {
System.out.println("Runs once after all tests in this class.");
}
}

📝 Output:
Runs once before all tests in this class.
Running Test A.
Running Test B.
Runs once after all tests in this class.

💾 @BeforeTest & @AfterTest


 Runs before and after <test> tag in testng.xml.
import org.testng.annotations.*;

public class TestLevelSetup {

@BeforeTest
public void beforeTest() {
System.out.println("Before <test> tag execution.");
}

@Test
public void sampleTest() {
System.out.println("Executing sample test.");
}

@AfterTest
public void afterTest() {
System.out.println("After <test> tag execution.");
}
}

🌟 @BeforeSuite & @AfterSuite


 Runs once before and after the entire suite (all tests).

import org.testng.annotations.*;

public class SuiteLevelTest {

@BeforeSuite
public void beforeSuite() {
System.out.println("Before the entire suite.");
}

@Test
public void testCase() {
System.out.println("Executing test case in the suite.");
}

@AfterSuite
public void afterSuite() {
System.out.println("After the entire suite.");
}
}
📝 Output:
Before the entire suite.
Executing test case in the suite.
After the entire suite.

⚡ 3. Advanced TestNG Annotations


🏃 @Test (priority) – Defines the execution order of tests.

import org.testng.annotations.Test;

public class PriorityTest {

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

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

@Test(priority = 3)
public void thirdTest() {
System.out.println("Third test executed.");
}
}

📝 Output:
First test executed.
Second test executed.
Third test executed.

⚠️@Test (dependsOnMethods) – Creates dependencies between test methods.


import org.testng.annotations.Test;

public class DependencyTest {

@Test
public void startApp() {
System.out.println("Application started.");
}

@Test(dependsOnMethods = {"startApp"})
public void login() {
System.out.println("Login successful.");
}

@Test(dependsOnMethods = {"login"})
public void performAction() {
System.out.println("Performed actions after login.");
}
}

📝 Output:
Application started.
Login successful.
Performed actions after login.

You might also like