This page is hosted for free by, if you are owner of this page, you can remove this message and gain access to many additional features by upgrading your hosting to PRO or VIP for just 41.60 UAH.
Do you want to support owner of this site? Click here and donate to his account some amount, he will be able to use it to pay for any of our services, including removing this ad.

TestNG Framework

images (2)imagesimages (1)

Talking about our requirement to introduce TestNG with WebDriver is that it provides an efficient and effective test result format that can in turn be shared with the stake holders to have a glimpse on the product’s/application’s health thereby eliminating the drawback of WebDriver’s incapability to generate test reports. TestNG has an inbuilt exception handling mechanism which lets the program to run without terminating unexpectedly.

Both TestNG and JUnit belong to the same family of Unit Frameworks where TestNG is an extended version to JUnit and is more extensively used in the current testing era.

Features of TestNG

  • Support for annotations
  • Support for parameterization
  • Advance execution methodology that do not require test suites to be created
  • Support for Data Driven Testing using Dataproviders
  • Enables user to set execution priorities for the test methods
  • Supports threat safe environment when executing multiple threads
  • Readily supports integration with various tools and plug-ins like build tools (Ant, Maven etc.), Integrated Development Environment (Eclipse).
  • Facilitates user with effective means of Report Generation using ReportNG

TestNG versus JUnit

There are various advantages that make TestNG superior to JUnit. Some of them are:

  • Advance and easy annotations
  • Execution patterns can be set
  • Concurrent execution of test scripts
  • Test case dependencies can be set

Annotations are preceded by a “@” symbol in both TestNG and JUnit.So now let us get started with the installation and implementation part.

Creating a TestNG test class

  1. In the Project tool window, right click the directory where you want to create a new test class.
  2. Select New | Java Class from the context menu.
  3. In the Create New Class dialog, specify the class name and click OK.
  4. In the editor, write the code for your test class. Use the TestNG annotations where necessary. For example, you may want to annotate the whole class or individual methods:
    @Test ()
    public class NewClassForTesting {}
    @DataProvider public Object[][] data(){ return new String[][]{ new String[]{"data1"}, new String[]{"data2"} }; }
    @Test(dataProvider = "data") public void test(String d ){ Assert.assertEquals("First Line \n Second Line", "Third Line \n Fourth Line");}

    Here is a quick overview of the annotations available in TestNG for @Test along with attributes.

    @Test Marks a class or a method as part of the test.
    alwaysRun If set to true, this test method will always be run even if it depends on a method that failed.
    dataProvider The name of the data provider for this test method.
    dataProviderClass The class where to look for the data provider. If not specified, the data provider will be looked on the class of the current test method or one of its base classes. If this attribute is specified, the data provider method needs to be static on the specified class.
    dependsOnGroups The list of groups this method depends on.
    dependsOnMethods The list of methods this method depends on.
    description The description for this method.
    enabled Whether methods on this class/method are enabled.
    expectedExceptions The list of exceptions that a test method is expected to throw. If no exception or a different than one on this list is thrown, this test will be marked a failure.
    groups The list of groups this class/method belongs to.
    invocationCount The number of times this method should be invoked.
    invocationTimeOut The maximum number of milliseconds this test should take for the cumulated time of all the invocationcounts. This attribute will be ignored if invocationCount is not specified.
    priority The priority for this test method. Lower priorities will be scheduled first.
    successPercentage The percentage of success expected from this method
    singleThreaded If set to true, all the methods on this test class are guaranteed to run in the same thread, even if the tests are currently being run with parallel=”methods”. This attribute can only be used at the class level and it will be ignored if used at the method level. Note: this attribute used to be called sequential (now deprecated).
    timeOut The maximum number of milliseconds this test should take.
    threadPoolSize The size of the thread pool for this method. The method will be invoked from multiple threads as specified by invocationCount.
    Note: this attribute is ignored if invocationCount is not specified