Bird
0
0

You want to run a setup method once before all tests in a TestNG class and a cleanup method after each test method. Which annotations should you use for these methods?

hard📝 Application Q15 of 15
Selenium Java - TestNG Integration
You want to run a setup method once before all tests in a TestNG class and a cleanup method after each test method. Which annotations should you use for these methods?
A@BeforeMethod for setup, @AfterMethod for cleanup
B@BeforeMethod for setup, @AfterClass for cleanup
C@BeforeClass for setup, @AfterMethod for cleanup
D@BeforeTest for setup, @AfterTest for cleanup
Step-by-Step Solution
Solution:
  1. Step 1: Identify annotation to run once before all tests

    @BeforeClass runs once before any test methods in the class.
  2. Step 2: Identify annotation to run after each test method

    @AfterMethod runs after each test method to clean up.
  3. Final Answer:

    @BeforeClass for setup, @AfterMethod for cleanup -> Option C
  4. Quick Check:

    BeforeClass = once before all, AfterMethod = after each test [OK]
Quick Trick: Use BeforeClass once, AfterMethod after each test [OK]
Common Mistakes:
MISTAKES
  • Using BeforeMethod for setup once only
  • Using AfterClass for cleanup after each test
  • Confusing BeforeTest with BeforeClass

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes