TestNG
It is developer tool used by development team to perform unit testing,
It is java base application.
Tester will use TestNG for automating test script.
Advantages of TestNG :
1)It provides parallel execution of test method.
2)Auto execution report generation.
3)TestNG allows grouping of test methods into test groups.
4)it allows to assign priority to test method.
5)It allows to define dependency of one test method over other method.
6)It allow to assign priority to test method.
Procedure to Install TestNG :
1) Go To Help and click on Eclipse marketplace
2)Enter TestNG in search field and search
3)Click on Install for TestNG
4)Follow installation step by clicking on Next and Finish.
Procedure to Config TestNG to Java Project :
1)Select Java Project and right click and select Properties
2)Select Java Build Path and click on Libraries
3)Select TestNG click on Apply and Finish and also click on Apply and Close.
4)Restart the Eclipse.
Example :
package TestNg;
import org.testng.annotations.Test;
@Test
public class TestNG_Demo{
public void m1()
{
System.out.println("execuite m1");
}
}
@Test : To execute method we use TestNG annotation @Test.
TestNG - Suite Test :
A test suite is a collection of test cases intended to test a behavior or a set of behaviors of software program.
Procedure to create Suite file :
1)Right click on Package and select TestNG
3)Click on Convert to TestNG and Finish.
Executing package from suite :
Example :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test thread-count="5" name="Test">
<classes>
<class name="package_1.TestCase_1"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Executing classes from different package :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test thread-count="5" name="Test">
<classes>
<class name="package_1.TestCase_1"/>
<class name="Demo_package.Script_1"></class>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Parameterization :
Passing parameters for argumented method by using TestNG annotations
@Dataprovider
@Parameter
- @Dataprovider :
This annotation is use to pass parameters or value from class level.
Example :
package testNg;
import org.testng.Reporter;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Dprovider {
@DataProvider
public Object[][]testData()
{
Object[][] rv=new Object[3][2];
rv[0][0]="admin1";
rv[0][1]="manager1";
rv[1][0]="admin2";
rv[1][1]="manager2";
rv[2][0]="admin3";
rv[2][1]="manager3";
return rv;
}
@Test(dataProvider="testData")
public void test1(String un,String pwd)
{
Reporter.log(un,true);
Reporter.log(pwd,true);
}
}
- @Parameter :
This annotation is use to pass test data from suite file.
package testNg;
import org.testng.Reporter;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
@Test
public class Parameterization {
@Parameters({"un","pwd"})
public void test1(String un,String pwd)
{
Reporter.log(un,true);
Reporter.log(pwd,true);
}
}
Assertion or verification in TestNG :
Asserts helps us to verify the conditions of the test and decide whether test has failed or passed.
1)assertEquals(): This method is use to verify expected result and actual result. If both result are same then verification will be pass.
2)assertNotEquals(): This method is use to verify expected result expected and actual result if both results are not same then verification will be pass.
3)assertTrue(): This method is use to check condition. If condition is true then verification will be pass.
4) assertFalse(): This method is use to check condition. If condition is fail then verification will be pass.
5)assertNull(): This method is use to verify variable is empty or not. If variable is empty then verification will be pass.
6)assertNotNull(): This method is use to verify variable is empty or not. If variable is not empty then verification will be pass.
- But in assertion, If one of the verification failed then other verification execution will be skip and to overcome this we use soft assert.
Soft assert :
Example :
package testNg;
import org.testng.Reporter;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
@Test
public class Asrt {
public void test1()
{
SoftAssert s=new SoftAssert();
Reporter.log("Test1 method execuition started",true);
String str1="Hello";
String str2="Hi";
s.assertEquals(str1, str2);
Reporter.log("verification 1 completed",true);
String str3="Hello";
s.assertEquals(str1, str3);
Reporter.log("verification 2 completed",true);
Reporter.log("Test method execuition is completed");
s.assertAll();
}
}
TestNG annotations :
- @BeforeSuite: This annotation is use to execute a method before suite file execution.
- @AfterSuite: This annotation is use to execute a method after suite file execution.
- @BeforeTest: This method is use to execute a method before suite file test tag execution.
- @AfterTest: This annotation is use to execute a method after suite file test tag execution.
- @BeforeGroups: This method will run before the first test run of that specific group.
- @AfterGroups: This method will run after all test methods of that group complete their execution.
- @BeforeClass: This annotation is use to execute a method before test class execution method.
- @AfterClass: This annotation is use execute a method after test class execution method.
- @BeforeMethod: This annotation is use to execute method before each test method execution.
- @AfterMethod: This annotation is use to execute a method before each test method execution.
Example :
package testNg;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class AnnotationsDemo {
@Test
public void testScript1() {
System.out.println("Executed Test script 1");
}
@AfterClass
public void afterClass() {
System.out.println("Executed Before After class Method");
}
@BeforeClass
public void beforeClass() {
System.out.println("Executed Before Before class Method");
}
@BeforeMethod
public void beforemethod() {
System.out.println("Executed Before Every Method");
}
@AfterMethod
public void aftermethod() {
System.out.println("Executed After Every Method");
}
@AfterTest
public void aftertest() {
System.out.println("Executed After Every Test method");
}
@BeforeTest
public void beforetest() {
System.out.println("Executed Before Every Test method");
}
@BeforeSuite
public void beforeSuite() {
System.out.println("Executed Before Test Suite Method");
}
@AfterSuite
public void afterSuite() {
System.out.println("Executed After Test Suite Method");
}
}
I Test Result :
I Test Result is an interface used to identify test method execuition status.
I Test Result method should be annotated with @AfterMethod annotation.
Example :
package testNg;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
@Test
public class Itest {
@Test
public void test1()
{
String str1="hello";
String str2="hii";
Assert.assertEquals(str1, str2);
}
@Test
public void test2()
{
String str1="hello";
String str2="hello";
Assert.assertEquals(str1, str2);
}
@AfterMethod
public void testResult(ITestResult result)
{
String testmethod_name = result.getName();
int status = result.getStatus();
if(status==1)
{
Reporter.log(testmethod_name+"method is pass",true);
}
else if(status==2)
{
Reporter.log(testmethod_name+"method is fail",true);
}
else
{
Reporter.log(testmethod_name+"method is skip",true);
}
}
}