TestNG SDET 2024 Most Asked Interview Questions
1. What is TestNG, and why is it used?
- Answer: TestNG is a testing framework inspired by JUnit but with more features. It is used for executing unit tests, functional tests, and integration tests. TestNG supports annotations, data-driven testing, parallel execution, and grouping of tests, making testing more efficient.
2. What are the key features of TestNG?
- Answer:
- Annotations (like
@Test
,@BeforeMethod
, etc.) - Grouping of test cases
- Parallel test execution
- Data-driven testing using
@DataProvider
- Detailed HTML reports
- Flexible configuration through XML files
- Annotations (like
3. What are TestNG annotations, and how do they work?
- Answer: TestNG annotations are used to control the flow of test execution. Common annotations include:
@Test
: Marks a method as a test.@BeforeMethod
: Runs before every test method.@AfterMethod
: Runs after every test method.@BeforeClass
: Runs before the first method in the class.@AfterClass
: Runs after all test methods in the class.
4. How do you run tests in parallel using TestNG?
- Answer: Parallel execution can be enabled in TestNG by setting the
parallel
attribute in the TestNG XML file. Example:
Here,<suite name="Parallel Test Suite" parallel="methods" thread-count="3"> <test name="Test1"> <classes> <class name="MyTestClass"/> </classes> </test> </suite>
parallel="methods"
will execute test methods in parallel, andthread-count
defines the number of threads.
5. What is the difference between @BeforeMethod
and @BeforeClass
?
- Answer:
@BeforeMethod
: Executes before each test method.@BeforeClass
: Executes once before all methods in the class.
6. How do you handle dependencies between test cases in TestNG?
- Answer: You can define dependencies using the
dependsOnMethods
ordependsOnGroups
attribute in the@Test
annotation. Example:
Here,@Test public void login() { // login test } @Test(dependsOnMethods = {"login"}) public void checkDashboard() { // checks after login }
checkDashboard()
will only run iflogin()
passes.
7. What is @DataProvider
in TestNG, and how is it used?
- Answer:
@DataProvider
is used to supply multiple sets of data to a test method, enabling data-driven testing. Example:
This test will run twice with different data sets.@DataProvider(name = "userData") public Object[][] getData() { return new Object[][] { {"user1", "pass1"}, {"user2", "pass2"} }; } @Test(dataProvider = "userData") public void loginTest(String username, String password) { // login using username and password }
8. How do you create and use a TestNG XML file?
- Answer: A TestNG XML file is used to configure and organize test cases, control test execution, and enable parallel execution. Example of a basic XML file:
You can run this XML by right-clicking the file in your IDE and selecting Run as TestNG Suite.<suite name="Suite1"> <test name="Test1"> <classes> <class name="com.example.MyTestClass"/> </classes> </test> </suite>
9. What is the difference between SoftAssert
and Assert
in TestNG?
- Answer:
Assert
: When a condition fails, the test immediately stops, and the following steps do not execute.SoftAssert
: The test continues even if the assertion fails. At the end of the test, you need to callsoftAssert.assertAll()
to mark the test as failed if any soft assertion failed.
10. How do you skip a test in TestNG?
- Answer: You can skip a test by using the
enabled
attribute in the@Test
annotation. Example:
Another way is to use@Test(enabled = false) public void skipThisTest() { // This test will be skipped }
throw new SkipException("Skipping test")
inside the test method.
0 Comments