TestNG Annotations and its uses

In this article we will go through some TestNG Annotations and their uses.

TestNG Annotations and its uses

  • @BeforeSuite
  • @BeforeTest
  • @BeforeClass
  • @BeforeMethod
  • @Test
  • @AfterMethod
  • @AfterClass
  • @AfterTest
  • @AfterSuite
  • @Guice
  • @Parameters
    • Added in testng.xml file with parameters and value tag after suite/ test tag.
    • Parameters defined at suite level will be applicable for all the tests by default
    • Parameters defined at test level will override the parameters defined at suite level.
    • Not suitable for large scale applications
  • @DataProvider
    • DataProvider is called in the test method with the name.
    • Uses external datasource to import the data.
    • If no DataProvider name is provided, it takes class name by default.

TestNG keywords for Test Method

  • priority
    • Can be -ve
    • Default priority is 0.
    • If priority is not mentioned, the sequence of the execution will be based upon the alphabetical order of methods.
    • Only supports @Test method
  • enabled
    • Can be either true or false
    • If defined as false, test method is skipped.
  • invocationCount
    • Number of times you want to execute a test method.
  • invocationTimeOut
    • Time period is defined for a certain method.
  • throw new SkipException("message")
    • Includes a method in skip count. Total TCs will showcase skip methods.
  • groups
    • Logically group the test cases
    • testng.xml has to be changed accordingly
    • A test method can be part of multiple groups.
    • Contains include and exclude keywords for the test methods.
    • Whatever name is defined in run tag gets executed.
    • Groups can be grouped together by using define keyword
    • Structure of testxng.xml is
<suite name = "Sample Suite">
    <test name = "Sample test">
        <groups>
            <run>
                <include name ="group1"></include>
                <include name="group2"></include>
            </run>
        </groups>
    <classes>
        <class name="PackageName.ClassName"></class>
    <classes>
    </test>
</suite>

<suite name = "Sample Suite">
    <test name = "Sample test">
        <groups>
            <define name = " ExecuteAllGroups>
                <include name ="group1"></include>
                <include name="group2"></include>
                <include name = "group3"> <include>
            </define>
            <run>
                <include name ="ExecuteAllGroups"></include>
                <exclude name = "group4"></exclude>
             </run>
        </groups>
    <classes>
        <class name="PackageName.ClassName"></class>
    <classes>
    </test>
</suite>


  • dependsOnMethods
  • dependsOnGroups
  • dataProviderClass
    • Used when your @DataProvider is present in another class and the reference is made from different class with the help of dataProviderClass.
  • thread-count and parallel
    • To run tests/ classes/ methods in parallel
    • Example syntax given below
    • Provided at the suite level.
    • Syntax of the same is given below
<suite name= "Sample suite" thread-count="2" parallel = "tests">
    <test name = "Sample test">
        <classes>
            <class name="class1"></class>
            <class name="class2"><class>
            <class name="class3"><class>
        <classes>
    </test>

    <test name ="test 2">
        <classes>
            <class name="class4"></class>
            <class name="class5"></class>
        </classes>
    </test>
</suite>

</suite>

  • threadPoolSize
    • Initiates multiple threads.
  • retryAnalyzer
    • Passed along with class name to re-execute the failed test cases. The class name which is passed must implement the interface IRetryAnalyzer.
  • Include or Exclude
    • Methods can be used with include or exclude keyword in a test suite.

Useful interface in TestNG

  • ITestContext
    • Used for creating custom reports.
  • IRetryAnalyzer
    • Implements re-execution of the failed test cases.
  • IAnnotationTransformer


Comments

Popular posts from this blog

Azure Tutorials Series - Azure Networking

Coforge Interview Questions | Automation Testing profile

Testing in CI/CD