TestNG Interview Questions

Showing 1 - 10 of 17 results

What is return type of TestNG Data Provider?

Data provider returns a two-dimensional JAVA object to the test method and the test method, will invoke M times in a M*N type of object array. For example, if the DataProvider returns an array of 2*3 objects, the corresponding test case will be invoked 2 times with 3 parameters each time.

Example:

/**
   * @return Object[][] where first column contains 'author'
   * and second column contains 'Country'
*/

    @DataProvider(name="SearchProvider")
    public Object[][] getData()
    {
        return new Object[][]
        {
            { "Ram", "India" },
            { "Sham", "UK" },
            { "Mohan", "USA" }
       };
    }

Explain what does @Test(invocationCount=?) and @Test(threadPoolSize=?) indicate.

@Test(invocationCount=?) is a parameter that indicates the number of times this method should be invoked. @Test(threadPoolSize=?) is used for executing suites in parallel. Each suite can be run in a separate thread.

To specify how many times @Test method should be invoked from different threads, you can use the attribute threadPoolSize along with invocationCount.

Example:

@Test(threadPoolSize = 3, invocationCount = 10)
public void testServer() 
{
}

How does TestNG allow you to state dependencies? Explain it with an example.

Dependency is a feature in TestNG that allows a test method to depend on a single or a group of test methods. Method dependency only works if the "depend-on-method" is part of the same class or any of the inherited base classes (i.e. while extending a class). 

@Test(groups={"Car"})
public void drive()
{
}
 
@Test(dependsOnMethods={“drive”}, groups={"Car"})
public void changeGear() 
{
}
 
@Test(dependsOnMethods={“changeGear”}, groups={“Car”})
public void accelerate()
{
}

Explain what is Group Test in TestNG?

In TestNG, methods can be categorized into groups. When a particular group is being executed, all the methods in that group will be executed.  We can execute a group by parameterizing it’s name in group attribute of @Test annotation.

Example:

@Test(groups={"Smoke"})
public void one()
{
}
 
@Test(groups={"Smoke"})
public void Two()
{
}
 
@Test(groups={"Sanity"})
public void Three()
{
}

What is soft assertion in Selenium? How can you mark a test case as failed by using soft assertion?

Soft Assertions are customized error handlers provided by TestNG. Soft Assertions do not throw exceptions when assertion fails, and they simply continue to the next test step. They are commonly used when we want to perform multiple assertions.

To mark a test as failed with soft assertions, call assertAll() method at the end of the test.

@Test
public void myTest() 
{
    SoftAssert softAssert = new SoftAssert();
    softAssert.assertTrue("");
    softAssert.assertTrue("");
    softAssert.assertAll();
}

How to skip a method or a code block in TestNG?

If you want to skip a particular test method, then you can set the "enabled" parameter in test annotation to false.

By default, the value of "enabled" parameter will be true. Hence it is not necessary to define the annotation as true while defining it.

Example:

@Test(enabled = false)
public myMethod()
{
}

Explain DataProviders in TestNG using an example. Can I call a single data provider method for multiple functions and classes?

DataProvider is a TestNG feature, which enables us to write DataDriven tests. When we say, it supports DataDriven testing, then it becomes obvious that the same test method can run multiple times with different data-sets. DataProvider is in fact another way of passing parameters to the test method.

@DataProvider marks a method as supplying data for a test method. The annotated method must return an Object[] where each Object[] can be assigned to parameter list of the test method.

To use the DataProvider feature in your tests, you have to declare a method annotated by @DataProvider and then use the said method in the test method using the "dataProvider" attribute in the Test annotation.

As far as the second part of the question is concerned, Yes, the same DataProvider can be used in multiple functions and classes by declaring DataProvider in separate class and then reusing it in multiple classes.

Example

@DataProvider(name="UserDetails")
public Object[][] getDataFromDataprovider()
{
    return new Object[][]
    {
        { "Ram", "India" },
        { "John", "UK" },
        { "Ben", "USA" }
    };
}


@Test(dataProvider="UserDetails")
public void testMethod(String name, String country)
{
}
Subscribe to TestNG Interview Questions