top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Defining prerequisites for test case and reusability of code using TestNG @BeforeMethod annotation.

TestNG is an open - source test automation framework where NG stands for 'Next Generation'. One of the advantages of this framework is that tests are more structured and readable. Different TestNG annotations support the testcases by controlling the flow of execution. To define them, methods need to be simply annotated with prefix ‘@‘ symbol e.g. @test.

Below is the list of TestNG annotations and how they are executed.


@BeforeSuite: The annotated method will be run before all tests in the suite have run.

@AfterSuite: The annotated method will be run after all tests in the suite have run.

@BeforeTest: The annotated method will be run before before first @Test annotated method.

@AfterTest: The annotated method will be run after when all @Test annotated methods complete the execution of those classes which are inside <test> tag in testng.xml file.

@BeforeClass: The annotated method run before the first test method is executed and it runs one time per class

@AfterClass: The annotated method will run after all the test methods in the current class have been run.

@BeforeMethod: The annotated method will be run before each test method annotated with @test.

@AfterMethod: The annotated method will be run after each test method.


@Test: The annotated method is a test case


What is Code Reusability and how can we achieve it through TestNG annotations?


Code reusability means that code is written once and is reused multiple times for different testcases or across the framework wherever required . Let's take an example of an application under test that requires a user to perform below steps repeatedly to reach to the point where actual testing starts afterwards.


Rewriting same code repeatedly is not a good practice and it slows down the development time ,productivity and make code bulky. Let's take an example of TestNG's @BeforeMethod annotation that can be used to handle such scenarios .It will run the above code before every testcase annotated with @test . So if there are 10 test cases , @BeforeMethod will run for every testcase and the user can focus on actual testing after this point.

Similarly to make the code look more neat, we can create test methods for above steps and call them in @BeforeMethod to perform same task. Now let's see one more example from Page object model, where we have called different methods under @BeforeMethod to define prerequisites for the application. Methods can be called from other classes by creating the object of the classes they belong to and methods can be called directly too. There is no need to extend to any class to use methods.








Conclusion: Now that we know that annotations are easy to use and understand ,we can use it in a very easy way to control the flow of execution and define the prerequisites for the test cases in our project.

52 views0 comments
bottom of page