TestNG Tutorial
Introduction to TestNG Configuration and set up of TestNG project Simple TestNG project in IntelliJ IDEA TestNG annotations Assertions in TestNG Difference between assertEquals and assertSame Ignoring the tests in TestNG Grouping the tests timeOut tests in TestNG Exception tests in TestNG dependsOnMethods dependsOnGroups Hard and Soft dependencies Passing parameters using XML file Passing parameters using Data providers Dynamic Data Providers group-by-instances attribute in TestNG XML suite file Test priorities in TestNG invocationCount in TestNG Executing Selenium tests using TestNG Executing TestNG tests using main method Executing JUnit tests from within TestNG Running tests in Parallel in TestNG Executing only failed tests in TestNG TestNG integration with Maven TestNG integration with Gradle Reporter class in TestNG TestNG Reports Execution Listener Test Listener Comparison between JUnit and TestNGAnnotations in testng
Here is the list of all TestNG annotations.- @Test – used to mark a test method.
- @BeforeTest – method to be executed before executing set of tests (test tag in XML suite)
- @AfterTest – method to be executed after executing set of tests (test tag in XML suite)
- @BeforeMethod – method to be executed before every test method
- @AfterMethod – method to be executed after every test method
- @BeforeClass – method to be executed before executing all test methods in a test class
- @AfterClass – method to be executed after executing all test methods in a test class
- @BeforeSuite – method to be executed before all tests in the suite
- @AfterSuite – method to be executed after all tests in the suite
- @BeforeGroups – method to be executed before a test belonging to specific group
- @AfterGroups – method to be executed after a test belonging to specific group
- @DataProvider – data generation for tests
- @Parameters – pass parameters to tests
- @Factory –
- @Listeners –
package org.softpost;
import org.testng.annotations.*;
public class Annotations {
@BeforeSuite
public void beforeSuite(){
System.out.println("This method is invoked before suite");
}
@AfterSuite
public void afterSuite(){
System.out.println("This method is invoked after suite");
}
@BeforeTest
public void beforeTest(){
System.out.println("This method is invoked before test tag");
}
@AfterTest
public void afterTest(){
System.out.println("This method is invoked after test tag");
}
@BeforeMethod
public void beforeTestMethod(){
System.out.println("This method is invoked before test method");
}
@AfterMethod
public void afterTestMethod(){
System.out.println("This method is invoked after test method");
}
@BeforeClass
public void beforeClassMethod(){
System.out.println("This method is invoked before class");
}
@AfterClass
public void afterClassMethod(){
System.out.println("This method is invoked after class");
}
@BeforeGroups(groups = {"sanity"})
public void beforeGroupsMethod(){
System.out.println("This method is invoked before sanity group");
}
@AfterGroups(groups = {"sanity"})
public void afterGroupsMethod(){
System.out.println("This method is invoked after sanity group");
}
@Test(groups = "sanity")
public void test1(){
System.out.println("This is a test1");
}
}
Here is the output of above code.
[TestNG] Running:
C:UsersSagar.IdeaIC15system emp-testng-customsuite.xml
This method is invoked before suite
This method is invoked before test tag
This method is invoked before class
This method is invoked before sanity group
This method is invoked before test method
This is a test1
This method is invoked after test method
This method is invoked after sanity group
This method is invoked after class
This method is invoked after test tag
This method is invoked after suite
===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
Web development and Automation testing
solutions delivered!!