---Advertisement---

TestNG Interview Questions and Answers (Level-2)

By Manisha

Published On:

---Advertisement---

11. How can we make one test method dependent on others using TestNG?

Ans.
In TestNG, we can use the dependsOnMethods parameter within the @Test annotation to make one test method dependent on others. This ensures that the dependent test runs only after the successful execution of the specified method(s).
Example:

java

@Test(dependsOnMethods = { “preTests” })

public void testMethod() {

  // Test logic

}

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

Ans.
The priority parameter in the @Test annotation is used to set the priority of test cases in TestNG. Tests with lower priority values are executed first. This allows fine-grained control over the order of execution of test cases. Example:

java

@Test(priority = 1)

public void testMethod() {

  // Test logic

}

Ques. 13. What is the default priority of test cases in TestNG?

Ans.
If no priority is specified for a test method in TestNG, the default priority is set to 0. Tests with lower priority values (closer to 0) are executed first.
Example:

java

@Test(priority = 1)  // Executes after the default test with priority 0

public void testMethodWithPriority() {

  // Test logic

}

Ques. 14. How can we run a Test method multiple times in a loop (without using any data provider)?

Ans.
We can use the invocationCount attribute to execute a test method multiple times. Setting invocationCount to an integer value allows the method to run that many times in a loop.
Example:

java

@Test(invocationCount = 10)

public void invocationCountTest() {

  // Test logic

}

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

Ans.
The threadPoolSize attribute defines the number of threads allocated for executing a test method concurrently. This is particularly useful when combined with the invocationCount parameter to parallelize test execution. Example:

java

@Test(threadPoolSize = 5, invocationCount = 10)

public void threadPoolTest() {

  // Test logic

}

This will run the test method 10 times using 5 threads concurrently.

Ques. 16. What is the difference between soft assertion and hard assertion in TestNG?

Ans.
In TestNG:

  • Hard assertions immediately stop the test execution upon failure of any assertion (the default behavior).
  • Soft assertions allow test execution to continue even if an assertion fails, providing the ability to collect all assertion results and determine the test’s overall success or failure at the end.

Example of soft assertion:

java

@Test

public void softAssertionTest() {

  SoftAssert softAssert = new SoftAssert();

  softAssert.fail(“This is a soft failure”);

  softAssert.assertTrue(true);

  softAssert.assertAll();  // Collects results and reports test outcome

}

Ques. 17. How to fail a TestNG test if it doesn’t get executed within a specified time?

Ans.
The timeOut attribute of the @Test annotation specifies the maximum time allowed for a test to complete. If the test method doesn’t complete within the given time, it will fail with a TimeoutException. Example:

java

@Test(timeOut = 1000)

public void timeOutTest() throws InterruptedException {

  Thread.sleep(2000);  // Will fail due to timeout

}

Ques. 18. How can we skip a test case conditionally?

Ans.
To skip a test case conditionally in TestNG, we can throw a SkipException. This will mark the test as skipped in the execution report and prevent the test from running further. Example:

java

@Test

public void testMethod() {

  if (someCondition) {

    throw new SkipException(“Skipping this test case.”);

  }

  // 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 in the @Test annotation, we can ensure that a test method will run even if the test methods it depends on fail or are skipped. Example:

java

@Test

public void parentTest() {

  Assert.fail(“This test fails”);

}

@Test(dependsOnMethods = {“parentTest”}, alwaysRun = true)

public void dependentTest() {

  System.out.println(“This test runs even if the parent test fails.”);

}

Ques. 20. How can we pass parameters to the test script using TestNG?

Ans.
TestNG allows us to pass parameters to the test methods using the @Parameters annotation and the parameter tag in the testng.xml file. Example of testng.xml:

xml

<suite name=”sampleTestSuite”>

  <test name=”sampleTest”>

    <parameter name=”sampleParamName” value=”sampleParamValue”/>

    <classes>

      <class name=”TestFile” />

    </classes>

  </test>

</suite>

Example of test method:

java

public class TestFile {

  @Test

  @Parameters(“sampleParamName”)

  public void parameterTest(String paramValue) {

    System.out.println(“Value of sampleParamName is – ” + paramValue);

  }

}

👉The Next 10 Questions-3: TESTNG

---Advertisement---

Leave a Comment