TestNG中BeforeClass和BeforeTest的区别

Difference between BeforeClass and BeforeTest in TestNG

正如我们从 TestNG 官方文档中了解到的那样:

@BeforeClass: 被注释的方法将在 运行 当前 class 中的第一个测试方法被调用之前。

@BeforeTest:<test> 标签内属于 class 的任何测试方法是 运行 之前,注释方法将是 运行。 13=]

以上两个 TestNG 注释在功能上看起来很相似。谁能解释一下独特的区别?

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");
  }
}

如果您有以下测试套件...

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

... 那么输出 [为了便于阅读而缩进] 将是

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

希望对您有所帮助:)

如果您从另一个 class 扩展,结果如下:

parentTest - BeforeTest- parent     
testClass1 - BeforeTest- test1    
parentTest - BeforeClass- parent    
testClass1 - BeforeClass- test1    
parentTest - BeforeMethod- parent    
testClass1 - BeforeMethod- test1    
testClass1 - myTestMethod1    
testClass1 - AfterMethod- test1    
parentTest - AfterMethod- parent    
parentTest - BeforeMethod- parent    
testClass1 - BeforeMethod- test1    
testClass1 - myTestMethod2    
testClass1 - AfterMethod- test1    
parentTest - AfterMethod- parent
testClass1 - AfterClass- test1    
parentTest - AfterClass- parent
testClass1 - AfterTest- test1
parentTest – AfterTest- parent

我的意见:

@BeforeClass:被注解的方法会在运行当前class中的第一个测试方法被调用之前

@BeforeTest:被注释的方法将运行在当前套件中的任何测试方法之前是运行

@BeforeMethod - 在每个测试方法之前执行,例如使用@Test注解的方法

@BeforeTest - 仅在 testng.xml 文件中给出的标记之前执行。

简而言之,@BeforeMethod 适用于 Java 类 中定义的测试。 并且 @BeforeTest 适用于 testng.xml 中定义的测试,即 XML 文件。

在解释差异之前,首先这是一些测试术语

Test suite – 由一个或多个测试标签组成。

Test tag - 包含一项或多项测试 classes.

Test class – 由一种或多种方法组成。

例如

<suite name="suit1">
  <test name="TestTag1">
    <classes>
      <class name="TestClass1"/>
    </classes>
  </test>
  <test name="TestTag2">
    <classes>
      <class name="TestClass2"/>
      <class name="TestClass3"/>
    </classes>
  </test>
</suite>

@BeforeTest : 它只会在任何测试标签之前调用一次,无论该标签内有多少测试 class 或用 @Test 注释的方法有多少,它每个测试标签只会被调用一次,在前面的XML例子中@BeforeTest会被调用两次,一次在TestTag1之前,第二次在TestTag2之前,所以它可以用于初始化一个测试标签内不同测试 classes 之间的公共对象。

@BeforeClass : 在任何测试class之前只会被调用一次,不管里面有多少方法注解了@Test这个测试class,每个测试class只会调用一次,在前面的XML例子中@BeforeClass会被调用三次,在[=24之前调用一次] =] 第二次在 TestClass2 之前,第三次在 TestClass3 之前,所以它可以用来初始化一个测试中不同测试方法之间的公共对象 class.

@BeforeSuite 将被调用一次 suit1 花色

调用顺序如下

@BeforeSuite
    @BeforeTest
        @BeforeClass
            @BeforeMethod
                @Test

想了解更多@BeforeMethod,请参考答案

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

The annotations above will also be honored (inherited) when placed on a superclass of a TestNG class. This is useful for example to centralize test setup for multiple test classes in a common superclass.

In that case, TestNG guarantees that the "@Before" methods are executed in inheritance order (highest superclass first, then going down the inheritance chain), and the "@After" methods in reverse order (going up the inheritance chain).

要了解有关 TestNG 注释的更多信息:https://testng.org/doc/documentation-main.html#annotations

以上答案中缺少的是 @BeforeClass@BeforeTest 注释之间的用法差异。

很明显,用@BeforeClass注释的方法(最常见的是setup方法)将在class中编写的所有测试用例之前只执行一次。并且用'@BeforeTest'注释的方法将在每个测试用例之前执行,而不管它们的count/sequence/underline逻辑如何。

因此,当我们的方法调用时间长且执行时间长并且这些调用的输出不会被任何测试用例更改时,使用 @BeforeClass。例如,在设置方法中得到一个 API 响应,它将被所有测试使用。 然而,当我们需要做一些清理工作时使用 @BeforeTest@,并且每个测试都需要一些新资源来开始。例如新建一个订单等

这是 TestNG 层次结构: 测试套件 -> 测试 -> Class -> 方法

@BeforeTest 在测试标签内所有 classes 中的所有方法执行之前执行一次。

另一方面;

@BeforeClass在class定义的所有方法执行之前执行一次。

我已将脚本上传到我的 GitHub 个人资料中。这是相同的 link。喜欢就点个赞吧

Please have a look at github: