Loading

TestNG Framework


Introduction :

It is a framework developed with advanced features in it. Similar to Junit and Nunit frameworks, it covers a wide range of tests - unit, functional, end-to-end, integration etc. NG stands for next generation.

Few noticeable features of this framework are :

  • Annotation Support -Annotations simply means a label that specifies a task to be performed prior execution of rest of the code. This can be explained with the help of the following code snippet.

    @test

    public static void main()

    {

    System.out.println("test case 1");

    }

    @aftermethod

    public static void main()

    {

    System.out.println("will execute after every method");

    }

    @beforetest

    public static void main()

    {

    System.out.println("this will execute before the test");

    }

    The output will be as follows :

    this will execute before the test

    test case 1

    will execute after every method

    In the above example we have used a very simple strategy to show the flow of execution of the code on the basis of annotations. The "@before" annotation will execute first, then the "@test", and finally the “@after” annotation. If we declare a set of “before” annotations here, they will be the ones to be executed first followed by "test" annotation methods and then the "after" annotations.

    TestNG has a list of annotations in it. These are :

    • @BeforeSuite - the annoted method will run before running the test suites.
    • @AfterSuite - the annoted method will run only once after the execution of tests in the suite.
    • @BeforeClass - this annoted method will run only once before the firsr test method in the current class is invoked.
    • @AfterClass - this annoted method will run only once after the current class with test method finishes execution.
    • @BeforeTest - the annoted method will run before the test method.
    • @AfterTest - will run after the annoted test method runs.
    • @BeforeGroups - this will run before the first test method executes.
    • @AfterGroups - this method will run after the last test method.
    • @BeforeMethod - the annoted method will run before each test method.
    • @AfterMethod - the annoted method will execute after each test method.
    • @DataProvider - supplies data for a test method.

      eg.

      @DataProvider(name="provider1")

      public Object[][] createdata()

      return new Object[][]{

      { "Alex" , new Integer(36) },

      { "Robin", new Integer(37) }

      };

      }

      @Test(dataProvider="provider1")

      public static verify(String n1,String n2)

      {

      System.out.println(n1+" "+n2);

      }

      The above code snippet shows how "DataProvider" annotation works. Let's understand in detail.

      1. The first method, that is, createData(), is the first method which shall execute. The Object[][] returns the data that is passed to it.

      2. The verify() method gets the data from the dataprovider,i.e, provider1.

      • @Factory - this must return an object, which will be used in test classes.
      • @Listeners - defines listeners.
      • @Parameters - specifies how a parameter is to be passed to a @Test method.
      • @Test - the test becomes a part of the class or a method.

    The use of annotations are primarily to prioritize the flow of execution of various tests.

    • Support for multiple tools -TestNG provides a support for a large number of tools and plug-ins . Some of the tools and plug-ins are Eclipse, IDEA, Maven etc.
    • Multi thread safe -It is robust enough to ensure that our program is thread safe, meaning , one thread per test class.
    • Report Generation -It provides ways to create a report of the progress using a feature named, ReportNG.
    • Prioritization and Parameterization -As we see in the above examples, it is observed that the annotations are a way to set priority of the flow of execution. And also we can pass parameters to the methods just as we do in normal programming as well.

    Why TestNG ?

    While we had test frameworks like Junit and Nunit, what provoked one to shift focus to another testing tool? The answer is simple. TestNG has a number of such features which facilitates ease of use. It has support for easy to use annotations which helps to prioritize the tasks in a systematic manner, it allows concurrent execution of test scripts, test case dependencies can be set.

    TestNG can be used in Eclipse IDE as follows :

    1. Open Eclipse, and select eclipse Marketplace option.
    2. In the dialogue box that appears, type in TestNG in the find tab, and click on go.
    3. Click on the install button.
    4. Confirm the installation.

    If you wish to create a sample TestNG project, then it can be done by selecting preferences in the Window tab, and select TestNG. After that create a Java Project, click on Add library and include the TestNG file. This way you can explore the TestNG framework and discover the hidden facts by yourself.