• 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

                Void test1(arg1)

                 {

                 }

                @parameters({"para1","para2"})
                @Test
                Void test2   (arg1,arg2)
                {
                 }
 

     TestNG Assertions:-

                TestNG Assertion are use to Implement functional validation by comparing the given             expected value with actual value. TestNG provided Assert class. This assert class                    provided
                            assertEquals()
                            assertTrue()
                            assertFalse()
                Methods to implement functional validations.
       assertEquals():-
                        It compares the given expected value with actual value. a both are same it                      produce the test result "Pass". If not same it produce result as "Fail"
       assertTrue():-
                        It is used to compare the expected condition true with actual condition. If both               are same It produce the results as "Pass". Otherwise "Fail".
      assertFalse():-
                        It is used to compare expected condition false with actual condition. If actual                   also false It produce the result's as "Pass". Otherwise "Fail".
 
      Commenting Testcases in the TestSuite:
            <!--code--> used to comment code in .xml or .html files
                <suite>
                        <test name = "Testcase1">
                        </test>
                <!--
                         <test name = "Testcase2">
                    </test>
                 -->
                </suite>
 
 
------------------------------------------------------------------------------------------------------------
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");
    }    
}