Difference between BeforeClass and BeforeTest in TestNG

后端 未结 7 2278
旧巷少年郎
旧巷少年郎 2020-11-28 05:00

As we know from official TestNG documentation:

@BeforeClass: The annotated method will be run before the first test method in the current class is invok

7条回答
  •  自闭症患者
    2020-11-28 05:49

    SeleniumAbstractTest.class

    public abstract class SeleniumAbstractTest {
    
      @BeforeSuite
      public void beforeSuite() {
        System.out.println("BeforeSuite");
      }
    
      @BeforeTest
      public void beforeTest() {
        System.out.println("BeforeTest");
      }
    
      @BeforeClass
      public void beforeClass() {
        System.out.println("BeforeClass");
      }
    
      @BeforeMethod
      public void beforeMethod() {
        System.out.println("BeforeMethod");
      }
    
      @AfterMethod
      public void afterMethod() {
        System.out.println("AfterMethod");
      }
    
      @AfterClass
      public void afterClass() {
        System.out.println("AfterClass");
      }
    
      @AfterTest
      public void afterTest() {
        System.out.println("AfterTest");
      }
    
      @AfterSuite
      public void afterSuite() {
        System.out.println("AfterSuite");
      }
    
    }
    

    MyTestClass1.class

    public class MyTestClass1 extends SeleniumAbstractTest {
    
      @Test
      public void myTestMethod1() {
        System.out.println("myTestMethod1");
      }
    
      @Test
      public void myTestMethod2() {
        System.out.println("myTestMethod2");
      }
    }
    

    MyTestClass2.class

    public class MyTestClass2 extends SeleniumAbstractTest {
    
      @Test
      public void myTestMethod3() {
        System.out.println("myTestMethod3");
      }
    
      @Test
      public void myTestMethod4() {
        System.out.println("myTestMethod4");
      }
    }
    

    If you have the following Test Suite...

    
      
        
           
        
      
    
      
        
          
          
        
      
    
    

    ... then the output [indented for easy reading] will be

    BeforeSuite
    '   BeforeTest
    '   '   BeforeClass
    '   '   '   BeforeMethod
    '   '   '   '   myTestMethod3
    '   '   '   AfterMethod
    '   '   '   BeforeMethod
    '   '   '   '   myTestMethod4
    '   '   '   AfterMethod
    '   '   AfterClass
    '   AfterTest
    '   BeforeTest
    '   '   BeforeClass
    '   '   '   BeforeMethod
    '   '   '   '   myTestMethod1
    '   '   '   AfterMethod
    '   '   '   BeforeMethod
    '   '   '   '   myTestMethod2
    '   '   '   AfterMethod
    '   '   AfterClass
    '   '   BeforeClass
    '   '   '   BeforeMethod
    '   '   '   '   myTestMethod3
    '   '   '   AfterMethod
    '   '   '   BeforeMethod
    '   '   '   '   myTestMethod4
    '   '   '   AfterMethod
    '   '   AfterClass
    '   AfterTest
    AfterSuite
    

    Hope it helps :)

提交回复
热议问题