0% found this document useful (0 votes)
39 views7 pages

TestNG Annotations With Concepts

Uploaded by

Akshaya
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)
39 views7 pages

TestNG Annotations With Concepts

Uploaded by

Akshaya
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/ 7

Annotations

1. @Test

 Purpose: Marks a method as a test method.

 Usage: This is the most important annotation in TestNG. The method annotated with @Test
will be executed as a test case.

 Example:

@Test

public void testMethod() {

System.out.println("This is a test method");

 Attributes:

o priority: Defines the order in which tests should be run.

o enabled: Enables or disables the test method.

o groups: Allows grouping of tests.

o dependsOnMethods: Specifies dependencies on other test methods.

2. @BeforeSuite

 Purpose: Executes before all tests in the suite.

 Usage: It can be used for setting up test environment configurations.

 Example:

@BeforeSuite

public void beforeSuite() {

System.out.println("Before Suite: Setup configurations");

3. @AfterSuite

 Purpose: Executes after all tests in the suite.

 Usage: Can be used for cleanup activities.

 Example:

@AfterSuite

public void afterSuite() {


System.out.println("After Suite: Cleanup activities");

4. @BeforeTest

 Purpose: Executes before any test methods in the test tag.

 Usage: Useful for setting up preconditions for tests.

 Example:

@BeforeTest

public void beforeTest() {

System.out.println("Before Test: Setup preconditions");

5. @AfterTest

 Purpose: Executes after all test methods in the test tag.

 Usage: Can be used to clean up after tests.

 Example:

@AfterTest

public void afterTest() {

System.out.println("After Test: Cleanup after tests");

6. @BeforeClass

 Purpose: Executes before the first method of the current class.

 Usage: Useful for setting up class-level resources.

 Example:

@BeforeClass

public void beforeClass() {

System.out.println("Before Class: Setup resources for class");

7. @AfterClass
 Purpose: Executes after all methods in the current class.

 Usage: Used for releasing class-level resources.

 Example:

@AfterClass

public void afterClass() {

System.out.println("After Class: Release resources for class");

8. @BeforeMethod

 Purpose: Executes before each test method.

 Usage: Use it to set up prerequisites for each test.

 Example:

@BeforeMethod

public void beforeMethod() {

System.out.println("Before Method: Setup before each test method");

9. @AfterMethod

 Purpose: Executes after each test method.

 Usage: Can be used for teardown after each test.

 Example:

@AfterMethod

public void afterMethod() {

System.out.println("After Method: Cleanup after each test method");

10. @DataProvider

 Purpose: Provides data for test methods.

 Usage: It can be used to run a test method multiple times with different data.

 Example:

@DataProvider(name = "data-provider")
public Object[][] dataProviderMethod() {

return new Object[][] {

{ "data1" }, { "data2" }

};

@Test(dataProvider = "data-provider")

public void testMethod(String data) {

System.out.println("Test Data: " + data);

11. @Listeners

 Purpose: Allows you to register listener classes.

 Usage: Used to modify the behavior of TestNG.

 Example:

@Listeners(MyListener.class)

public class MyTests {

// Test methods here

12. @Factory

 Purpose: Used to create test instances dynamically.

 Usage: It allows you to pass parameters to test classes.

 Example:

public class TestFactory {

@Factory

public Object[] createTests() {

return new Object[] { new TestClass1(), new TestClass2() };

}
13. @DependsOnGroups

 Purpose: Specifies the groups that must be executed before the current test.

 Usage: Useful for defining dependencies between test groups.

 Example:

@Test(dependsOnGroups = "group1")

public void testMethod() {

System.out.println("Test method depending on group1");

TestNG Methods

1. setUp()

 Purpose: Method used to set up preconditions before each test execution.

 Usage: Can be annotated with @BeforeMethod.

 Example:

@BeforeMethod

public void setUp() {

// Initialization code

2. tearDown()

 Purpose: Method used to clean up after each test execution.

 Usage: Can be annotated with @AfterMethod.

 Example:

@AfterMethod

public void tearDown() {

// Cleanup code

Configuration Methods

1. setParallel()

 Purpose: Configures parallel test execution.


 Usage: Typically used in the testng.xml file.

 Example:

xml

Copy code

<suite name="Suite" parallel="methods">

<test name="Test">

<classes>

<class name="MyTestClass"/>

</classes>

</test>

</suite>

Assertions

1. Assert.assertEquals(actual, expected)

 Purpose: Asserts that two values are equal.

 Example:

Assert.assertEquals(actualValue, expectedValue);

2. Assert.assertTrue(condition)

 Purpose: Asserts that a condition is true.

 Example:

Assert.assertTrue(condition, "Condition is not true");

3. Assert.assertFalse(condition)

 Purpose: Asserts that a condition is false.

 Example:

Assert.assertFalse(condition, "Condition is not false");

4. Assert.assertNotNull(object)

 Purpose: Asserts that an object is not null.

 Example:
Assert.assertNotNull(object, "Object is null");

TestNG XML Configuration

TestNG XML file

 Purpose: Used to configure test suites and tests.

 Usage: Helps to define test execution parameters.

 Example:

xml

Copy code

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Suite">

<test name="Test">

<classes>

<class name="MyTestClass"/>

</classes>

</test>

</suite>

Execution Order

Execution Order of Annotations

 TestNG follows a specific order of execution for annotations:

1. @BeforeSuite

2. @BeforeTest

3. @BeforeClass

4. @BeforeMethod

5. @Test

6. @AfterMethod

7. @AfterClass

8. @AfterTest

9. @AfterSuite

You might also like