Bird
0
0

Given the following TestNG class, what will be the output order?

medium📝 Predict Output Q4 of 15
Selenium Java - TestNG Integration
Given the following TestNG class, what will be the output order?
public class SampleTest {
  @BeforeMethod
  public void setup() { System.out.println("Setup"); }

  @Test
  public void test1() { System.out.println("Test1"); }

  @Test
  public void test2() { System.out.println("Test2"); }

  @AfterMethod
  public void teardown() { System.out.println("Teardown"); }
}
ASetup Test1 Test2 Teardown Teardown
BTest1 Setup Teardown Test2 Setup Teardown
CSetup Teardown Setup Test1 Test2 Teardown
DSetup Test1 Teardown Setup Test2 Teardown
Step-by-Step Solution
Solution:
  1. Step 1: Understand TestNG execution flow

    For each @Test method, @BeforeMethod runs first, then the test, then @AfterMethod.
  2. Step 2: Apply to given methods

    For test1: Setup -> Test1 -> Teardown; for test2: Setup -> Test2 -> Teardown; so combined output is Setup Test1 Teardown Setup Test2 Teardown.
  3. Final Answer:

    Setup Test1 Teardown Setup Test2 Teardown -> Option D
  4. Quick Check:

    @BeforeMethod and @AfterMethod run around each test = C [OK]
Quick Trick: Before and After run around each test method [OK]
Common Mistakes:
  • Assuming @BeforeMethod runs once for all tests
  • Mixing order of print statements
  • Thinking @AfterMethod runs after all tests

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes