TestNG中BeforeClass和BeforeTest的区别 [英] Difference between BeforeClass and BeforeTest in TestNG
问题描述
正如我们从官方 TestNG 文档中了解到的:
As we know from official TestNG documentation:
@BeforeClass:
注释的方法将在当前类中的第一个测试方法被调用之前运行.
@BeforeClass:
The annotated method will be run before the first test method in the current class is invoked.
@BeforeTest:
注释的方法将在任何属于 <test>
标签内的类的测试方法运行之前运行.
@BeforeTest:
The annotated method will be run before any test method belonging to the classes inside the <test>
tag is run.
上述两个 TestNG 注释在功能上看起来很相似.谁能解释一下独特的区别?
Both the above TestNG annotations look similar in functionality. Can anyone explain the unique difference?
推荐答案
SeleniumAbstractTest.class
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
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
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
希望有帮助:)
这篇关于TestNG中BeforeClass和BeforeTest的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!