Wednesday, 27 March 2024

Scenario Based Frequently Asked Interview Q&A on TestNG

Scenario: Handling Flaky Tests

Question: How would you deal with flaky tests in your Selenium automation suite using TestNG?

Answer: To address flaky tests, I would implement retry logic in TestNG. By using the retryAnalyzer feature in TestNG, I can specify a custom retry analyzer class that determines whether a failed test should be retried based on certain conditions, such as specific exceptions or test result statuses. This helps improve the reliability of the test suite by rerunning failed tests automatically.

Scenario: Parallel Execution

Question: Explain how you would implement parallel execution of tests in TestNG for faster execution in your Selenium automation framework.

Answer: TestNG allows running tests in parallel, either at the suite or test level, by organizing them into different suites and configuring parallel attributes like "parallel" and "thread-count".

<suite name="MyTestSuite" parallel="tests" thread-count="5">

<!-- Test configurations -->

</suite>

Scenario: Data-Driven Testing

Question: Describe how you would perform data-driven testing using TestNG in your Selenium automation framework.

Answer: TestNG supports data-driven testing through its @DataProvider annotation, which allows me to supply test data from external sources such as Excel sheets or databases. I can create a method annotated with @DataProvider to provide test data, and then annotate my test methods with @Test(dataProvider) to execute the tests with different data sets. This enables to execute the same test logic with multiple input values and verify the expected behavior.

@DataProvider(name = "loginData")

public Object[][] getLoginData() {

return new Object[][] {

{"user1", "pwd1"},

{"user2", "pwd2"},

};

}

@Test(dataProvider = "loginData")

public void loginTest(String username, String password){}

Scenario: Grouping and Tagging Tests

Question: How would you group and tag tests in TestNG for better organization and selective execution in your Selenium automation framework?

Answer: TestNG allows grouping tests using the @Test(groups) annotation, enabling selective execution and better organization of test suites by defining groups in testng.xml. This allows for selective execution of tests and better organization of test suites like smoke, sanity, regression.

@Test(groups = {"smoke", "regression"})

public void loginTest() {

// code

}

No comments:

Salesforce AI Associate Certification - 3

What is semantic retrieval in the context of LLMs?   Searching for relevant information in other data sources What additional protection do...