TestNG Interview Questions 1714161053
TestNG Interview Questions 1714161053
Kus
TESTNG INTERVIEW
QUESTIONS
@kushalparikh11
[email protected]
https://www.linkedin.com/in/kushalparikh11/
Kushal Parikh
QA Automation Engineer
Ques.5. How can we group test cases like separate test cases for the
Sanity suite, Regression suite, etc?
Ans. Using the groups attribute in TestNG, we can assign the test methods
to different groups.
//Test method belonging to sanity suite only
@Test(groups = {"sanitySuite"})
public void testMethod1() {
//Test logic
@KUSHALPARIKH11 1
Kushal Parikh
QA Automation Engineer
Ques.6. How can we exclude a Test method from getting executed via
the testng.xml file?
Ans. Using the exclude tag in the testng.xml file, we can exclude a
particular test method from getting executed.
<suite name="Test Suite" verbose="1">
<test name="TestName">
<classes>
<class name="TestClassName">
<methods>
<exclude name="testMethodToBeExcluded"/>
</methods>
</class>
</classes>
</test>
</suite>
@KUSHALPARIKH11 2
Kushal Parikh
QA Automation Engineer
@KUSHALPARIKH11 3
Kushal Parikh
QA Automation Engineer
@KUSHALPARIKH11 4
Kushal Parikh
QA Automation Engineer
the remaining test execution. The result of all the assertions can be collated
at the end using softAssert.assertAll() method.
@Test
public void softAssertionTest(){
SoftAssert softAssert= new SoftAssert();
//Assertion failing
softAssert.fail();
System.out.println("Failing");
//Assertion passing
softAssert.assertEquals(1, 1);
System.out.println("Passing");
//Collates test results and marks them pass or fail
softAssert.assertAll();
}
Here, even though the first assertion fails still the test will continue with
execution and print the message below the second assertion.
Hard assertions on the other hand are the usual assertions provided by
TestNG. In case of hard assertion in case of any failure, the test execution
stops, preventing the execution of any further steps within the test method.
Ques.19. How can we make sure a test method runs even if the test
methods or groups on which it depends fail or get skipped?
@KUSHALPARIKH11 5
Kushal Parikh
QA Automation Engineer
Ques.20. How can we pass the parameter to the test the script using
TestNG?
Ans. Using @Parameter annotation and the ‘parameter’ tag in testng.xml
we can pass parameters to test scripts.
Sample testng.xml –
<suite name="sampleTestSuite">
<test name="sampleTest">
<parameter name="sampleParamName" value="sampleParamValue"/>
<classes>
<class name="TestFile" />
</classes>
</test>
</suite>
@KUSHALPARIKH11 6
Kushal Parikh
QA Automation Engineer
}
//This method is bound to the above data provider returning 2D array of 3*2 matrix
//The test case will run 3 times with different set of values
@Test(dataProvider = "dataProvider1")
public void sampleTest(String s1, String s2) {
System.out.println(s1 + " " + s2);
}
//Constructor
public TestClass(String str) {
this.str = str;
}
@Test
public void TestMethod() {
System.out.println(str);
}
}
@KUSHALPARIKH11 7
Kushal Parikh
QA Automation Engineer
//The test methods in class TestClass will run twice with data "k1" and "k2"
@Factory
public Object[] factoryMethod() {
return new Object[] { new TestClass("K1"), new TestClass("k2") };
}
}
@KUSHALPARIKH11 8