3
3
Ans. TestNG(NG for Next Generation) is a testing framework that can be integrated with
selenium or any other automation tool to provide multiple capabilities like assertions, reporting,
parallel test execution, etc.
TestNG provides different assertions that help in checking the expected and actual results.
It provides parallel execution of test methods.
We can define the dependency of one test method over others in TestNG.
We can assign priority to test methods in Selenium.
It allows the grouping of test methods into test groups.
It allows data-driven testing using @DataProvider annotation.
It has inherent support for reporting.
It has support for parameterizing test cases using @Parameters annotation.
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.
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.
Ques.8. What is the order of execution of the test method based on the different annotations?
Ans. The test methods in TestNG follow the Suite->Test->Class->Method sequence combined
with the Before annotations->Test annotations->After annotations sequence. So, the order of
execution is-
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
@AfterTest
@AfterSuite
assertEquals(String actual, String expected, String message) and other overloaded data types
in parameter
assertNotEquals(double data1, double data2, String message) and other overloaded data types
in parameter
assertFalse(boolean condition, String message)
assertTrue(boolean condition, String message)
assertNotNull(Object object)
fail(boolean condition, String message)
true(String message)
Ques.11. How can we make one test method dependent on others using TestNG?
Ans. Using dependsOnMethods parameter inside @Test annotation in TestNG we can make
one test method run only after the successful execution of the dependent test method.
@Test(dependsOnMethods = { "preTests" })
@Test(priority=1)
Ques.14. How can we run a Test method multiple times in a loop(without using any data
provider)?
Ans. Using invocationCount parameter and setting its value to an integer value, makes the test
method runs n number of times in a loop.
@Test(invocationCount = 10)
public void invocationCountTest(){
//Test logic
}
@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.17. How to fail a testNG test if it doesn’t get executed within a specified time?
Ans. We can use the timeOut attribute of @Test annotation. The value assigned to this timeOut
attribute will act as an upper bound. If the test doesn’t get executed within this time frame then it
will fail with timeout exception.
@Test(timeOut = 1000)
public void timeOutTest() throws InterruptedException {
//Sleep for 2sec so that test will fail
Thread.sleep(2000);
System.out.println("Will throw Timeout exception!");
}
@Test
public void testMethod(){
if(conditionToCheckForSkippingTest)
throw new SkipException("Skipping the test");
//test logic
}
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?
Ans. Using the “alwaysRun” attribute of @Test annotation, we can make sure the test method
will run even if the test methods or groups on which it depends fail or get skipped.
@Test
public void parentTest() {
Assert.fail("Failed test");
}
@Test(dependsOnMethods={"parentTest"}, alwaysRun=true)
public void dependentTest() {
System.out.println("Running even if parent test failed");
}
Here, even though the parentTest failed, the dependentTest will not get skipped instead it will
get executed because of “alwaysRun=true”. In case, we remove the “alwaysRun=true” attribute
from @Test then the report will show one failure and one skipped test, without trying to run the
dependentTest method.
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>
@Listeners(PackageName.CustomizedListenerClassName.class)
public class TestClass {
WebDriver driver= new FirefoxDriver();@Test
public void testMethod(){
//test logic
}
}
//Constructor
public TestClass(String str) {
this.str = str;
}
@Test
public void TestMethod() {
System.out.println(str);
}
}