【问题标题】:Difference between BeforeClass and BeforeTest in TestNGTestNG中BeforeClass和BeforeTest的区别
【发布时间】:2015-08-15 17:40:23
【问题描述】:

我们从 TestNG 官方文档中了解到:

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

@BeforeTest: 注释的方法将在属于<test> 标记内的类的任何测试方法运行之前运行。

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

【问题讨论】:

  • 每个班级一次与每个测试用例一次,我认为你可以在一个班级中有多个测试用例?
  • 这两个注解用于在执行所有测试用例之前启动。这就是相似之处。除此之外还有什么独特的吗?
  • 你有1个Testclass,有3个Testmethods:@BeforeClass会被执行一次,@BeforeTest会被执行3次。这就是区别。 @BeforeClass可用于搭建测试环境,@BeforeTest可用于清理数据或设置数据等...
  • @Dude 如果是这样,'@BeforeMethod' 有什么作用?实际上'@BeforeClass'和'@BeforeTest'都将执行一次:)'@BeforeMethod'将执行3次。是的,数据清理是主要的。
  • 好吧我错了,但答案说得很清楚,很完美

标签: selenium selenium-webdriver testng


【解决方案1】:

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

@BeforeTest 在 test 标签内所有类的所有方法执行之前执行一次。

另一方面;

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

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

请看: https://github.com/provivek2124/TestAnnotate.git1

【讨论】:

    【解决方案2】:

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

    很明显,带有@BeforeClass 注释的方法(最常见的是setup 方法)只会在类中编写的所有测试用例之前执行一次。并且带有'@BeforeTest'注释的方法将在每个测试用例之前执行,无论它们的计数/序列/下划线逻辑如何。

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

    【讨论】:

      【解决方案3】:
      @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

      【讨论】:

        【解决方案4】:

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

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

        Test tag - 由一个或多个测试类组成。

        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 : 在任何测试标签之前只会被调用一次,无论该标签内有多少个测试类或有多少用@Test注解的方法,每个测试标签只会被调用一次,在前面的 XML 示例中,@BeforeTest 将被调用两次,一次在TestTag1 之前,第二次在TestTag2 之前,因此它可以用于在一个测试标签内初始化不同测试类之间的公共对象。

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

        @BeforeSuite 会为suit1 套装调用一次

        调用顺序如下

        @BeforeSuite
            @BeforeTest
                @BeforeClass
                    @BeforeMethod
                        @Test
        

        想了解更多@BeforeMethod,请参考答案https://stackoverflow.com/a/52331616/1973933

        【讨论】:

          【解决方案5】:

          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
          

          希望对你有帮助:)

          【讨论】:

          • 这个答案的底部正是我要找的,太棒了!
          【解决方案6】:

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

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

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

          【讨论】:

            【解决方案7】:

            我的意见:

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

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

            【讨论】:

              【解决方案8】:

              如果你从另一个类扩展,结果如下:

              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
              

              【讨论】:

                猜你喜欢
                • 2018-11-21
                • 1970-01-01
                • 2018-03-16
                • 2012-01-22
                • 1970-01-01
                • 2023-03-24
                • 1970-01-01
                • 2021-05-26
                • 1970-01-01
                相关资源
                最近更新 更多