What is the difference between BeforeTest and BeforeMethod in TestNG

前端 未结 9 924
南旧
南旧 2020-12-10 01:31

Both annotations runs before the @test in testNG then what is the difference between two of them.

9条回答
  •  抹茶落季
    2020-12-10 01:44

    check below code and output

    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    
    public class Test_BeforeTestAndBeforeMethod {
    
        @BeforeTest
        public void beforeTest()
        {
            System.out.println("beforeTest");
        }
    
        @BeforeMethod
        public void beforeMethod()
        {
            System.out.println("\nbeforeMethod");
        }
    
    
        @Test
        public void firstTest()
        {
            System.out.println("firstTest");
        }
    
        @Test
        public void secondTest()
        {
            System.out.println("secondTest");
        }
    
        @Test
        public void thirdTest()
        {
            System.out.println("thirdTest");
        }
    }
    

    output:

    beforeTest
    
    beforeMethod
    firstTest
    
    beforeMethod
    secondTest
    
    beforeMethod
    thirdTest
    

提交回复
热议问题