2、TestNG常用注解
有必要介紹一下TestNG注解的生命周期,先看一下官網支持的注解有 :
注解 描述
@BeforeSuite 在該套件的所有測試運行之前運行,僅運行一次
@AfterSuite 在該套件的所有測試運行之后運行,僅運行一次
@BeforeTest 注釋的方法將在屬于test標簽內的所有類的所有測試方法運行之前運行,一個test標簽內可能有多個class(類),在當前test標簽內僅運行一次
@AfterTest 注釋的方法將在屬于test標簽內的所有類的所有測試方法運行之后運行,一個test標簽內可能有多個class(類),在當前test標簽內僅運行一次
@BeforeClass 在調用當前類的第一個測試方法之前運行,在當前類中僅運行一次
@AfterClass 在調用當前類的第一個測試方法之后運行,在當前類中僅運行一次
@BeforeGroups 配置方法將在組列表運行之前運行。 此方法保證在調用屬于這些組中的任何第一個測試方法之前不久運行
@AfterGroups 此配置方法將在組列表運行之后運行。該方法保證在調用屬于這些組的任何最后一個測試方法之后不久運行
@BeforeMethod 注釋方法將在當前類中的每個測試方法之前運行
@AfterMethod 注釋方法將在當前類中的每個測試方法之后運行
@Parameters 描述如何將參數傳遞給@Test方法
@DataProvider 標記一種方法來提供測試方法的數據。 注釋方法必須返回一個Object [] [],其中每個Object []可以被分配給測試方法的參數列表。 要從該DataProvider接收數據的@Test方法需要使用與此注釋名稱相等的dataProvider名稱
@Listeners 定義測試類上的偵聽器
@Factory 將一個方法標記為工廠,返回TestNG將被用作測試類的對象。 該方法必須返回Object []
@Test 將類或方法標記為測試的一部分,此標記若放在類上,則該類所有公共方法都將被作為測試方法
如上列表中的@Factory、@Linsteners這兩個是不常用的;
前十個注解看起來不太容易區分,順序不太容易看明白,以如下范例做簡單說明,代碼:
```
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class NewTest {
@Test(groups="group1")
public void test1() {
System.out.println("test1 from group1");
Assert.assertTrue(true);
}
@Test(groups="group1")
public void test11() {
System.out.println("test11 from group1");
Assert.assertTrue(true);
}
@Test(groups="group2")
public void test2()
{
System.out.println("test2 from group2");
Assert.assertTrue(true);
}
@BeforeTest
public void beforeTest()
{
System.out.println("beforeTest");
}
@AfterTest
public void afterTest()
{
System.out.println("afterTest");
}
@BeforeClass
public void beforeClass()
{
System.out.println("beforeClass");
}
@AfterClass
public void afterClass()
{
System.out.println("afterClass");
}
@BeforeSuite
public void beforeSuite()
{
System.out.println("beforeSuite");
}
@AfterSuite
public void afterSuite()
{
System.out.println("afterSuite");
}
//只對group1有效,即test1和test11
@BeforeGroups(groups="group1")
public void beforeGroups()
{
System.out.println("beforeGroups");
}
//只對group1有效,即test1和test11
@AfterGroups(groups="group1")
public void afterGroups()
{
System.out.println("afterGroups");
}
@BeforeMethod
public void beforeMethod()
{
System.out.println("beforeMethod");
}
@AfterMethod
public void afterMethod()
{
System.out.println("afterMethod");
}
}
```
運行結果如下:
```
beforeSuite
beforeTest
beforeClass
beforeGroups
beforeMethod
test1 from group1
afterMethod
beforeMethod
test11 from group1
afterMethod
afterGroups
beforeMethod
test2 from group2
afterMethod
afterClass
afterTest
PASSED: test1
PASSED: test11
PASSED: test2
===============================================
Default test
Tests run: 3, Failures: 0, Skips: 0
===============================================
afterSuite
```
由此可見,testng運行時,順序是這樣的:
@BeforeSuite->@BeforeTest->@BeforeClass->{@BeforeGroups[@BeforeMethod->@Test1->@AfterMethod,@BeforeMethod->@Test2->@AfterMethod, ...]@AfterGroups, @BeforeGroups[@BeforeMethod->@Test3->@AfterMethod,@BeforeMethod->@Test4->@AfterMethod, ...]@AfterGroups, ...}->@AfterClass->@AfterTest->@AfterSuite
其中{}內將@BeforeGroups@AfterGroups將@Test分組,每個分組內包含多個@Test。