11. How To Set Test Case Priority In Testng?
Answer: We use priority attribute to the @Test annotations. In case priority is not set then the test scripts execute in alphabetical order.

package TestNG;
import org.testng.annotations.*;
public class PriorityTestCase{
@Test(priority=0)
public void testCase1() {
system.out.println(“Test Case 1”);
}
@Test(priority=1)
public void testCase2() {
system.out.println(“Test Case 2”);
}
}
Output:
Test Case 1
Test Case 2


12. What Is Parameterized Testing In Testng?
Answer: Parameterized tests allow developers to run the same test over and over again using different values. There are two ways to set these parameters:

using testng.xml
using @DataProvider


13. How Can We Create Data Driven Framework Using Testng?
Answer: By using @DataProvider annotation,  we can create a Data Driven Framework.

@DataProvider(name=”getData”)
public Object[][] getData(){
//Object [][] data = new Object [rowCount][colCount];
Object [][] data = new Object [2][2];
data [0][0] = “FirstUid”;
data [0][1] = “FirstPWD”;
data[1][0] = “SecondUid”;
data[1][1] = “SecondPWD”;
return data;
}


14. How To Run A Group Of Test Cases Using Testng?
Answer: TestNG allows you to perform sophisticated groupings of test methods. Not only can you declare that methods belong to groups, but you can also specify groups that contain other groups. Then TestNG can be invoked and asked to include a certain set of groups (or regular expressions) while excluding another set.  This gives you maximum flexibility in how you partition your tests and doesn’t require you to recompile anything if you want to run two different sets of tests back to back.

Groups are specified in your testng.xml file and can be found either under the <test> or <suite> tag. Groups specified in the <suite> tag apply to all the <test> tags underneath.

@Test (groups = { “smokeTest”, “functionalTest” })
public void loginTest(){
System.out.println(“Logged in successfully”);
}


15. How To Create Group Of Groups In Testng?
Answer:Groups can also include other groups. These groups are called MetaGroups. For example, you might want to define a group all that includes smokeTest and functionalTest.

Let’s modify our testng.xml file as follows:

<groups>
<define name=”all”>
<include name=”smokeTest”/>
<include name=”functionalTest”/>
</define>
<run>
<include name=”all” />
</run>
</groups>


16. How To Run Test Cases In Parallel Using Testng?
Answer: We can use “parallel” attribute in testng.xml to accomplish parallel test execution in TestNG

The parallel attribute of suite tag can accept four values:

tests: All the test cases inside <test> tag of testng.xml file will run parallel

classes: All the test cases inside a java class will run parallel

methods: All the methods with @Test annotation will execute parallel

instances: Test cases in same instance will execute parallel but two methods of two different instances will run in different thread.

<suite name=”softwaretestingmaterial” parallel=”methods”>


17. How To Exclude A Particular Test Method From A Test Case Execution?
Answer: By adding the exclude tag in the testng.xml

<classes>
<class name=”TestCaseName”>
<methods>
<exclude name=”TestMethodNameToExclude”/>
</methods>
</class>
</classes>


18. How To Exclude A Particular Test Group From A Test Case Execution?
Answer: By adding the exclude tag in the testng.xml

<groups>
<run>
<exclude name=”TestGroupNameToExclude”/>
</run>
</groups>


19. How To Disable A Test Case In Testng?
Answer: To disable the test case we use the parameter enabled = false to the @Test annotation.

@Test(enabled = false)


20. How To Skip A @test Method From Execution In Testng?
Answer: By using throw new SkipException()

Once SkipException() thrown, remaining part of that test method will not be executed and control will goes directly to next test method execution.

throw new SkipException(“Skipping – This is not ready for testing “);


Leave a Reply