Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
916 views
in Technique[技术] by (71.8m points)

selenium - Difference between BeforeClass and BeforeTest in TestNG

As we know from official TestNG documentation:

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

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.

Both the above TestNG annotations look similar in functionality. Can anyone explain the unique difference?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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...

<suite name="Suite">
  <test name="Test1" >
    <classes>
       <class name="MyTestClass2" />
    </classes>
  </test>

  <test name="Test2">
    <classes>
      <class name="MyTestClass1"/>
      <class name="MyTestClass2"/>
    </classes>
  </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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...