gpt4 book ai didi

selenium - TestNG中BeforeClass和BeforeTest的区别

转载 作者:行者123 更新时间:2023-12-03 01:09:33 28 4
gpt4 key购买 nike

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

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

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

上述两个 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

关于selenium - TestNG中BeforeClass和BeforeTest的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30587454/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com