0% found this document useful (0 votes)
24 views5 pages

TestNG MCQ

The document consists of a series of questions and answers related to TestNG, a testing framework for Java. It covers various features of TestNG, such as annotations, parameter handling, test execution order, and exception handling. The answers provide insights into the advantages of TestNG over JUnit and the functionality of different annotations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

TestNG MCQ

The document consists of a series of questions and answers related to TestNG, a testing framework for Java. It covers various features of TestNG, such as annotations, parameter handling, test execution order, and exception handling. The answers provide insights into the advantages of TestNG over JUnit and the functionality of different annotations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

1. What is the main advantage of using TestNG over JUnit?

A) TestNG supports parallel execution of tests.


B) TestNG does not require annotations.
C) TestNG runs only unit tests.
D) TestNG does not support parameterized tests.

2. Which annotation is used to execute a method before all tests in a suite?


A) @BeforeTest
B) @BeforeSuite
C) @BeforeClass
D) @BeforeMethod

3. What is the correct TestNG annotation to run a method after each test case?
A) @AfterSuite
B) @AfterTest
C) @AfterClass
D) @AfterMethod

4. How can you pass parameters from a TestNG XML file to a test method?
A) Using @DataProvider
B) Using @Parameters
C) Using @Test
D) Using @Optional

5. In TestNG, how do you ignore a test method without removing it from the code?
A) Remove the @Test annotation.
B) Use enabled=false in the @Test annotation.
C) Use @Ignore annotation.
D) Comment out the method code.

@Test(enabled = false)
public void ignoredTest() {
System.out.println("This test will be ignored.");
}
6. Which TestNG feature is used to run the same test with different sets of data?
A) @Factory
B) @DataProvider
C) @Parameters
D) @BeforeTest

7. What will happen if the dependsOnMethods attribute is used, but the dependent
method fails?
A) The dependent method will be skipped.
B) The dependent method will run regardless.
C) The dependent method will also fail.
D) The test execution will stop.

8. What does the priority attribute in the @Test annotation define?


A) The order in which test methods will be executed.
B) The number of times a test should be retried.
C) The timeout period for a test.
D) The grouping of test methods.

9. Which TestNG XML tag specifies the classes that contain test methods?
A) <test>
B) <suite>
C) <class>
D) <method>

10. What is the default execution order of the following annotations in TestNG?
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
@AfterTest
@AfterSuite

11. How can you execute test methods in parallel in TestNG?


A) By setting parallel="methods" in the <suite> tag of the TestNG XML file.
B) By using @Test(parallel = true).
C) By setting thread-count in the <test> tag only.
D) By using @ParallelTest annotation.

12. What happens when alwaysRun=true is used in a TestNG annotation?


A) The test will run only when dependencies pass.
B) The test will always run even if dependent methods fail.
C) The test will never run if disabled.
D) It has no effect.

13. What is the default value of the invocationCount attribute in the @Test
annotation?
A) 0
B) 1
C) 2
D) Unlimited

14. How do you handle expected exceptions in a TestNG test method?


A) Using @Test(expectedExceptions = Exception.class)
B) Using try-catch block only
C) Using @HandleException
D) TestNG does not support this feature

15. How can you provide optional parameters in TestNG?


A) Using @DataProvider
B) Using @Optional with @Parameters
C) Using default values in the method signature
D) It’s not possible in TestNG

16. Which of the following allows grouping test methods in TestNG?


A) groups attribute in the @Test annotation.
B) groupByMethods attribute in the @Test annotation.
C) @TestGroup annotation.
D) Using <group> tag in TestNG XML.

17. How can you ensure a test runs multiple times with different data sets in
TestNG?
A) @Factory
B) @DataProvider
C) @Test(invocationCount = x)
D) @RepeatTest

18. What happens if you set priority to the same value for multiple @Test methods?
A) Tests run alphabetically by method name.
B) Tests run in an undefined order.
C) Tests run in the order they are defined in the class.
D) Tests fail due to a conflict.

19. What is the effect of timeOut in the @Test annotation?


A) Specifies how long the entire test suite can run.
B) Defines the maximum execution time for a test method.
C) It waits for the given time before running the test.
D) No effect unless invocationCount is also set.

20. What is the purpose of the @Factory annotation in TestNG?


A) To create test objects dynamically.
B) To group tests together.
C) To run the same test multiple times with different data sets.
D) To control parallel execution.
1.Answer: ✅ A) TestNG supports parallel execution of tests.
2.Answer: ✅ B) @BeforeSuite
3.Answer: ✅ D) @AfterMethod
4.Answer: ✅ B) Using @Parameters
5.Answer: ✅ B) Use enabled=false in the @Test annotation.
6.Answer: ✅ B) @DataProvider
7.Answer: ✅ A) The dependent method will be skipped.
8.Answer: ✅ A) The order in which test methods will be executed.
9.Answer: ✅ C) <class>
10.Answer:
✅ The correct order is:
@BeforeSuite → @BeforeTest → @BeforeClass → @BeforeMethod → @Test → @AfterMethod →
@AfterClass → @AfterTest → @AfterSuite
11.Answer: ✅ A) By setting parallel="methods" in the <suite> tag of the TestNG XML
file.
12.Answer: ✅ B) The test will always run even if dependent methods fail.
13.Answer: ✅ B) 1
14.Answer: ✅ A) Using @Test(expectedExceptions = Exception.class)
15.Answer: ✅ B) Using @Optional with @Parameters
16.Answer: ✅ A) groups attribute in the @Test annotation.
17.Answer: ✅ B) @DataProvider
18.Answer: ✅ A) Tests run alphabetically by method name.
19.Answer: ✅ B) Defines the maximum execution time for a test method.
20.Answer: ✅ A) To create test objects dynamically.

You might also like