Sunday, July 7, 2024

FAQ Where can we use @BeforeTest, @BeforeMethod in project?

FAQ Where can we use @BeforeTest, @BeforeMethod in project?

login()  -- pre condition

tc1

tc2

tc3

logout() -- post condition

 

Ex: if we want to run pre condition/ pre requirement only once, We will go for @BeforeTest annontation.

 ex:  if we want to run pre condition/ pre requirement for each Test Method/ multiple times, We will go for @BeforeMethod annonation

@BeforeTest:

Use @BeforeTest to perform setup tasks that need to be executed before any tests in a specific test suite. In TestNG, a test suite typically groups related test classes or test methods.

For example, you can use @BeforeTest to set up a database connection, initialize a web browser, or perform any other global setup that is common to all the test methods within a test suite.

 

@BeforeTest

public void setUpBeforeTest() {

    // Perform setup tasks for the entire test suite

    // e.g., initialize WebDriver, database connection, etc.

}

 

@BeforeMethod:

Use @BeforeMethod to perform setup tasks that need to be executed before each test method within a test class. This annotation ensures that the setup code is run before each individual test method.

Common use cases for @BeforeMethod include setting up test data, preparing the application state, or initializing objects required for the specific test.

 

@BeforeMethod

public void setUpBeforeMethod() {

    // Perform setup tasks before each test method

    // e.g., initialize objects, set up test data, etc.

}

FAQ:   testNG annotation Programs

Note:

 Order of execution:   is similar to testng.xml 

            <suite

                        <test

                                    <classes

                                                <methods>

                          ==

 

Before suite >  before Test > Before Class > Before method > @Test >

 After Method > AfterClass> AfterTest >AfterSuite


No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...