TestNG is an automation testing framework where NG stands for the “Next Generation”. It is based on the Java programming language and used for unit, integration, end-to-end, functional testing.
TestNG is derived from junit (java) and Nunit (.Net). TestNG has multiple classes, interfaces and methods which will make testers’ tasks easy.
Advantage of TestNG:
- It is easy to understand and use.
- TestNG has multiple classes, interfaces and methods that will make testers’ tasks easy.
- It has Annotations, and are easier to understand. Using this we can do parallel testing.
- It produces the HTML reports for implementation.
- By using this, test cases can be grouped more easily.
- It is support for Data-driven testing( @ DataProvider)
- It will supported by a variety of tools and plugins.
- It allows defining dependency of the test method over other methods.
- It allows to assign the priority to test methods.
- We can do test groups by using the grouping of test methods.
- It has support for parameterizing test cases using @ ParameterAnnotations.
- It acts as a compiler for our java code. So we don’t need to write a public static void main(string[]args). Internally it uses Java only.
TestNG Annotations:
Annotation is a form of metadata that can be added to the java source code.
- @Test.
- @BeforeSuite.
- @After Suite
- @BeforeTest.
- @BeforeClass.
- @BeforeMethod.
- @Test Case.
- @AfterMethod.
- @AfterClass.
- @AfterTest.
Let’s explore how these Annotations work.
- @BeforeSuite: It will run only one time at the beginning of all tests in the suite are executed.
- @AfterSuite: It will execute at the end of all tests in the suite are completed.
- @BeforeTest: This annotation will be executed before the first @Test annotation method. BeforeTest will be executed many times before the test case.
- @BeforeClass: It will be executed only one time throughout the test case and will be executed before the first @ Test method of execution.
- @AfterClass: It will be executed after all the test methods in the class have been executed.
- @BeforeMethod: It will be executed before every @test annotation method.
- @AfterMethod: It will be executed after every @test annotation method.
- @AfterTest:This will be executed when all @Test annotation methods complete the execution of those classes inside the <test> tag in the TestNG.xml file.
- @BeforeGroups: This method will execute before the first test run of the specific group.
- @AfterGroups: This method will be executed after all the test methods of that group complete their execution.
Let’s understand how to use these annotations:
public class testngAnnotations {
// Test Case 1
@Test
public void test1() {
System.out.println(“Test Case 1”);
}
// Test Case 2
@Test
public void test2() {
System.out.println(“Test Case 2”);
}
@BeforeMethod
public void beforeMethod() {
System.out.println(“Before Method”);
}
@AfterMethod
public void afterMethod() {
System.out.println(“After Method”);
}
@BeforeClass
public void beforeClass() {
System.out.println(“Before Class”);
}
@AfterClass
public void afterClass() {
System.out.println(“After Class”);
}
@BeforeTest
public void beforeTest() {
System.out.println(“Before Test”);
}
@AfterTest
public void afterTest() {
System.out.println(“After Test”);
}
@BeforeSuite
public void beforeSuite() {
System.out.println(“Before Suite”);
}
@AfterSuite
public void afterSuite() {
System.out.println(“After Suite”);
}
}
Annotations always starts with an executing suit and ends by executing method. This is how we use TestNG Annotations.
Annotations are make Selenium test scripts are more manageable, sophisticated and effective. By using these annotations it will be more helpful for testers, and makes their lives much easier.
Assertions in TestNG:
Assertions in TestNG are a way to verify that expected results and the actual results matched or not. Asserts help us to verify the conditions of the test and decide whether the test has failed or passed.
Different types of Asserts in TestNG
There are two types of TestNg Assert:
- Hard Assert
- Soft Assert
Hard Asserts are the asserts that stop the test execution when an assert statement fails, and the subsequent assert statements are therefore not validated. It throws the exception immediately upon failure of assertion.
Soft asserts Will Collects all the asserts encountered when running the @Test methods and it will not be included by default in the TestNG framework. These soft asserts keep on running even though one assert validation fails means the test execution does not stop.
Commonly used TestNG Assert Methods:
- Assert.assertEqual(String actual, String expected): In this we pass the actual string value and the expected string value as a parameters. Then we validates if the actual and expected values are the same or not.
- Assert.assertEqual(String actual, String expected, String message): when the assertion fails, the message displays along with exception thrown.
- Assert.assertEquals(boolean actual, boolean expected): It will takes two boolean values as input and validates if they are equal or not.
- Assert.assertTrue(condition): In this method of asserts, if the condition is true or not. If not, then the exception will throw the error.
- Assert.assertTrue(condition, message): Similar to the previous method with an addition of message, which is shown on console when this assertion fails along with the exception.
- Assert.assertFalse(condition): In this method of asserts, if the condition is false or not. If not, then it will throws an exception error.
- Assert.assertFalse(condition, message): It is similar to the previous method but with an addition of a message string which is shown on the console when the assertion fails, i.e., the condition is true*.
- public static void assertEquals(Object actual, Object expected, String message): This Asserts whether the two objects passed are equal or not. If not, the message and the exception error will appears. The message parameter is optional.
- public static void assertEquals(String actual, String expected, String message): This Asserts whether two strings are equal or not. If not, the message and along with the exception error will displays. The message parameter is optional.