Can you explain about TestNG's beforeClass, beforeMethod, afterClass, beforeSuite and afterSuite?

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

1 Answer

answered by (171 points)
0 votes
As the name says,

beforeClass - This is used to invoke before class activities, in case you are using one test per class, or yo - Thu want to execute all tests in class in one browser you can initiate browser instence at class level.

afterClalss- If you are using before class, we use afterClass to complte activity we started.

beforeMethod - In case of you need to start certain activity before method, such as initiating browser, logging in, initiating reporting instence, before each test, we use beforeMethod.

afterMethod- This is used in case to complete certain actions to be completed after @Test method is over

beforeSute - This is utilized to initiate stuff before Suite, such as reading TestNG.xml file and getting list of parameters, initiating report directory, purging cache, etc.

afterSuite This is utilized to initiate after execution activity such as writing report files, zipping and emailing the same.