What is the difference between BeforeTest and BeforeMethod in TestNG

前端 未结 9 925
南旧
南旧 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:42

    @BeforeTest : It will call Only once, before Test method.

    @BeforeMethod It will call Every time before Test Method.

    Example:

    import org.testng.annotations.AfterClass;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    
    public class Test_BeforeTestAndBeforeMethod {
    
        @BeforeTest
        public void beforeTestDemo()
        {       
            System.out.println("This is before test calling.");   
        }
        
        @BeforeClass
        public void beforeClassDemo()
        {
            System.out.println("This is before class calling.");
        }
    
        @BeforeMethod
        public void beforeMethodDemo()
        {
            System.out.println("This is before method calling.");
        }
        
        @Test
        public void testADemo()
        {
            System.out.println("This is Test1 calling.");
        }
    
        @Test
        public void testBDemo()
        {
            System.out.println("This is Test2 calling.");
        }
    
        @Test
        public void testCDemo()
        {
            System.out.println("This is Test3 calling.");
        }
        
        @AfterMethod
        public void afterMethodDemo()
        {
            System.out.println("This is after method calling.");
        }
        
        @AfterClass
        public void afterClassDemo()
        {
            System.out.println("This is after class calling.");
        }
        
        @AfterTest
        public void afterTestDemo()
        {
            System.out.println("This is after test calling.");
        }
    }
    

提交回复
热议问题