0
0
Selenium Javatesting~5 mins

TestNG annotations (@Test, @BeforeMethod, @AfterMethod) in Selenium Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @Test annotation in TestNG?
The @Test annotation marks a method as a test case that TestNG will run. It tells TestNG this method contains test code to execute.
Click to reveal answer
beginner
Explain the role of @BeforeMethod in TestNG.
@BeforeMethod runs before each @Test method. It is used to set up preconditions or initialize resources needed for the test.
Click to reveal answer
beginner
What does the @AfterMethod annotation do in TestNG?
@AfterMethod runs after each @Test method. It is used to clean up or release resources after the test finishes.
Click to reveal answer
intermediate
In what order do @BeforeMethod, @Test, and @AfterMethod run?
For each test method, TestNG runs @BeforeMethod first, then the @Test method, and finally @AfterMethod.
Click to reveal answer
intermediate
Why is it useful to use @BeforeMethod and @AfterMethod instead of putting setup and cleanup code inside @Test methods?
Using @BeforeMethod and @AfterMethod avoids repeating setup and cleanup code in every test. It keeps tests clean and easier to maintain.
Click to reveal answer
Which TestNG annotation is used to mark a method as a test case?
A@Test
B@BeforeMethod
C@AfterMethod
D@BeforeClass
When does a method annotated with @BeforeMethod run?
AAfter each @Test method
BBefore all tests in the class
CBefore each @Test method
DAfter all tests in the class
What is the main purpose of @AfterMethod?
ATo skip tests
BTo run tests
CTo initialize test data
DTo clean up after each test
If you have 3 @Test methods, how many times will @BeforeMethod run?
A3
B1
C2
DDepends on the test
Which annotation would you use to prepare test data before each test?
A@AfterMethod
B@BeforeMethod
C@Test
D@AfterClass
Describe how @BeforeMethod, @Test, and @AfterMethod work together in a TestNG test class.
Think about what happens before, during, and after each test method.
You got /3 concepts.
    Explain why using @BeforeMethod and @AfterMethod can improve test code quality compared to putting setup and cleanup inside @Test methods.
    Consider how repeating code affects your tests.
    You got /3 concepts.