• Slider Banner

    Prepare for

    Technical Interview

    Over .1 Million+ Interview Questions for different profiles

    Read More
  • Slider Banner

    Join the

    Programming World

    Learn about top programming languages, tools and frameworks.

    Read More
  • Slider Banner

    Prepare with

    Certification MCQ

    Practice for certification from 1000+ multiple choice questions.

    Read More

Differentiate between TDD and BDD.

The following are the main differences between the Behavior Driven Development and Test Drive Development:

Point of distinction BDD TDD
Development process is centred on Behavior is centred around Test
Written using Gherkin Language and programming language Programming Languages like Ruby or Java
Readbility Readable by both programmer and non-programmer. Both can work on same project Only programmer can work on TDD project

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)
{
}

What is parameterization in TestNG? How to pass parameters using testng.xml?

Parameterization is the technique of defining values in testng.xml file and sending them as parameters to the test class. This technique is especially useful when we need to pass multiple login credentials of various test environments. Take a look at the code below, in which “userName” is annotated as a parameter.

public class ParameterizedTest1
{
    @Test
    @Parameters("userName")
    public void parameterTest(String userName)
    {
        System.out.println("Parameterized value is : " + userName);
    }
}

To pass parameters using testng.xml file, we need to use "parameters" tag. Look at the below code for example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "<a href="http://testng.org/testng-1.0.dtd">http://testng.org/testng-1.0.dtd</a>" >
<suite name=”CustomSuite">
    <test name=”CustomTest”>
        <parameter name="userName" value=”Rohan"/>
        <classes>
            <class name="ParameterizedTest1" />
        </classes> 
    </test>
</suite>
Subscribe to