TestNG Interview Questions

ArtOfTesting

Ques. How can we disable or prevent a test case from running?

Ans. By setting the “enabled” attribute as false, we can disable a test method from running.

@Test(enabled = false) public void testMethod1() { //Test logic }

TestNG Interview Questions

ArtOfTesting

Ques. 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" })

TestNG Interview Questions

ArtOfTesting

Ques. How can we set the priority of test cases in TestNG?

Ans. We can define the priority of test cases using the “priority” parameter in @Test annotation. The tests with lower priority value will get executed first. Example-

@Test(priority=1)

TestNG Interview Questions

ArtOfTesting

Ques. 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 to run n number of times in a loop.

@Test(invocationCount = 10) public void invocationCountTest(){ //Test logic }

TestNG Interview Questions

ArtOfTesting

Ques. What is threadPoolSize? How can we use it?

Ans. The threadPoolSize attribute specifies the number of threads to be assigned to the test method. This is used in conjunction with invocationCount attribute. The number of threads will get divided with the number of iterations of the test method specified in the invocationCount attribute.

@Test(threadPoolSize = 5, invocationCount = 10) public void threadPoolTest(){ //Test logic }

TestNG Interview Questions

Ques. How can we skip a test case conditionally?

Ans. Using SkipException, we can conditionally skip a test case. On throwing the skipException, the test method is marked as skipped in the test execution report and any statement after throwing the exception will not get executed.

@Test public void testMethod(){ if(conditionToCheckForSkippingTest) throw new SkipException("Skipping the test"); //test logic }