- Advantages of TestNG over JUnit
- Why do we need TestNG in Selenium?
- Installing TestNG in Eclipse
- Creating a New TestNG Test File
- TestNG annotations
- TestNG assertions
- Running the TestNG Test
- Checking reports created by TestNG
- Creating multiple Tests
- Prioritizing Tests
- Parameterizing Tests with @dataProvider
- TestNGdataProvider with Excel
- Creating and Running Test Suites with TestNG.xml
- Sequential Test Execution with TestNG
- Parallel Test Execution with TestNG
TestNG :
TestNG is a testing framework provided following features to simplify automation testscripts.
1. Annotations
2. Assertions
3. Parameters
4. Test Reports
5. Test Suite.
TestNG Annotations:
TestNG Provided
@Test
@BeforeTest
@AfterTest
@BeforeSuite
@AfterSuite
Annotations to execute testcases and also to configure pre-conditions and post-conditions for a Testcases.
@Test:
If any method is marked with @Test Annotaion TesTNG executes that method then generate Testreport.
If the method executed successfully with no error's, TestNG generate Test Report as "Pass". If any error's Occured during test execution then TestNG generate test report as "Fail".
@BeforeTest:
A method Configured with @BeforeTest will be executed before executing each testcase present in the TestSuite.
@AfterTest:
A method configured with @AfterTest will be executed after executing each testcase present in the TestSuite.
@BeforeSuite:
A method configured with @BeforeSuite will be executed once at the end of the TestSuite.
@AfterSuite:
A method configured with @AfterSuite will be executed once at the end of the TestSuite.
TestNG TestSuite:
In TestNG we can create a TestSuite by grouping multiple testcases using TestNG.XML file
So that If we run a TestSuite TestNG will execute all Testcases present in the TestSuite Onebyone. then produce Test summery Report.
Syntax(TestSuite):-
<suite name = "TestSuite Title"><test name = "testcase title"><classes><class name = "class path"></class></classes></test></suite>A Sample TestSuite:-
<suite name = "Gmail TestSuite"><test name = "Send mail Testcase"><classes><class name = "testcases.sendmailTest"></class></classes></test><test name = "Recieve mail Testcase"><classes><class name = "testcases.recievemailTest"></class></classes></test></suite>TestNG Parameters:-
Passing input data two testcases is called parameterization.They are two steps to be followed to implement TestNG Parameterization.Step 1 Defineing Parameter's in TestSuite:<suite><test name = " Testcase title"><parameter name = "paraname" value = "paravalue"></parameter></test></suite>Step 2 Accessing Parameter's in Test method using @parameter's annotation:
@parameter's ({"para name"})
@Test
{
}
TestNG Assertions:-
30-12-2024
------------------------------------------------------------------------------------------------------------
TestNG Annotations:
-----------------------------------------------------------------------------------------------------------
TestNG provided
@Test
@BeforeTest
@AfterTest
@BeforeSuite
@AfterSuite
----------------------------------------------------------------------------------------------------------
Creating Test Suite
----------------------------------------------------------------------------------------------------------
<suite name="TestSuite Title">
<test name="TestCase Title">
<classes>
<class name="class path"></class>
</classes>
</test>
</suite>
-----------------------------------------------------------------------------------------------------------
A Sample Test Suite
-----------------------------------------------------------------------------------------------------------
<suite name="GMail TestSuite">
<test name="Send Mail TestCase">
<classes>
<class name="testcases.SendMailTest"></class>
</classes>
</test>
<test name="Receive Mail TestCase">
<classes>
<class name="testcases.ReceiveMailTest"></class>
</classes>
</test>
</suite>
----------------------------------------------------------------------------------------------------------
31-12-2024
----------------------------------------------------------------------------------------------------------
TestNG Parameters
-----------------------------------------------------------------------------------------------------------
There are 2 steps to be followed to implement TestNG parameterization.
Step 1 : Defining parameters in Test Suite:
<suite>
<test name = "TestCase Title">
<parameter name="paraname" value="paravalue"></parameter>
</test>
</suite>
Step 2 : Accessing Parameters in @Test method using @Parameters annotation:
@Parameters({"paraname"})
@Test
void test1(arg1)
{
}
@Parameters({"para1","para2"})
@Test
void test2(arg1,arg2)
{
}
------------------------------------------------------------------------------------------------------------
TestNG Assertions:
------------------------------------------------------------------------------------------------------------
TestNG provided Assert Class, This Assert Class provided
assertEquals()
assertTrue()
assertFalse()
methods to implment functional validations.
----------------------------------------------------------------------------------------------------------
assertEquals()
----------------------------------------------------------------------------------------------------------
public class DemoTest
{
@Test
void test1()
{
String exptitle,acttitle;
exptitle = "Google";
acttitle = "Google";
Assert.assertEquals(acttitle, exptitle);
}
@Test
void test2()
{
String exptitle,acttitle;
exptitle = "Google";
acttitle = "GMail";
Assert.assertEquals(acttitle, exptitle);
}
}
---------------------------------------------------------------------------------------------------------
assertTrue()
---------------------------------------------------------------------------------------------------------
public class DemoTest
{
@Test
void test1()
{
boolean actres = true;
Assert.assertTrue(actres);
}
@Test
void test2()
{
boolean actres = false;
Assert.assertTrue(actres);
}
}
-----------------------------------------------------------------------------------------------------------
assertFalse()
-----------------------------------------------------------------------------------------------------------
public class DemoTest
{
@Test
void test1()
{
boolean actres=false;
Assert.assertFalse(actres);
}
@Test
void test2()
{
boolean actres=true;
Assert.assertFalse(actres);
}
}
----------------------------------------------------------------------------------------------------------
Scheduling Test Priorities using : priority keyword
----------------------------------------------------------------------------------------------------------
public class DemoTest
{
@Test(priority = 0)
void launchApp()
{
System.out.println("Launch the Application");
}
@Test(priority = 1)
void closeApp()
{
System.out.println("Close the Application");
}
}
---------------------------------------------------------------------------------------------------------
Disabling / Suspending Test Methods from Test Execution
---------------------------------------------------------------------------------------------------------
public class DemoTest
{
@Test
void test1()
{
System.out.println("TestCase 1");
}
@Test(enabled = true)
void test2()
{
System.out.println("TestCase 2");
}
@Test(enabled = false)
void test3()
{
System.out.println("TestCase 3");
}
}
0 Comments