注釋
@Before和@After 注釋
這兩個就比較多,一般用于在測試構(gòu)件上。關(guān)于測試構(gòu)件以后詳細介紹,測試構(gòu)件一般有測試之前的操作和測試運行之后的清除數(shù)據(jù)的操作
Before分類有@BeforeSuite,@BeforeTest, @BeforeGroups,@BeforeClass,@BeforeMethod
After分類有@AfterSuite,@ AfterTest, @AfterGroups,@ AfterClass,@ AfterMethod
每個注釋的范圍不同而已
舉例:
package com.java.learn;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* create by Anthony on 2017/10/29
*/
public class TestNG_Demo1 {
@BeforeClass
public void setUp(){
System.out.println("啟動測試的前提條件準備,一般放這個方法中");
}
@AfterClass
public void tearDown(){
System.out.println("測試運行結(jié)束后的步驟,一般是恢復(fù)環(huán)境到測試開始之前的狀態(tài)");
}
@Test //執(zhí)行測試
public void test1(){
System.out.println("Hello");
}
}
@DataProvider注釋
標記一個方法是用來給其他測試方法提供數(shù)據(jù)的方法。要求該方法必須返回是object[][]
@Factory
標記這個方法是一個工廠,方法必須返回的是一個對象
@Listeners
在一個測試方法上定義一個監(jiān)聽。例如Selenium自動化測試中需要用到這個事件監(jiān)聽功能,方便我們輸出日志
@Parameters
描述了如何給一個測試方法傳提參數(shù)
例子:
package com.java.learn;
import com.sun.tracing.ProviderName;
import org.testng.annotations.*;
/**
* create by Anthony on 2017/10/29
*/
public class TestNG_Demo1 {
@Test
@Parameters({"Browser", "Server"})
public void test1(String browser, String server){
System.out.println("Hello");
System.out.println("這次啟動瀏覽器是: "+browser+" 測試服務(wù)器地址是: "+server);
}
}
項目根目錄下的testng.xml內(nèi)容如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default Suite">
<parameter name="Browser" value="Chrome" />
<parameter name="Server" value="https://www.baidu.com" />
<test name="Java_Learn">
<classes>
<class name="com.java.learn.TestNG_Demo1"/>
<!-- 添加不同測試類文件 -->
</classes>
</test> <!-- Java_Learn -->
</suite> <!-- Default Suite -->
@Test
在@Test下有很多選項可以設(shè)置,例如用例之間的依賴關(guān)系等
@Test注釋下的屬性
屬性timeOut
字面意思就是超時判斷,如果哪個測試方法需要監(jiān)聽執(zhí)行的時間,那么就可以考慮采用timeOut屬性,監(jiān)控時間的功能。
例如,實際的接口測試中,加入登錄接口時間不能超過3秒中。下面來看看如何監(jiān)控這個方法如果運行時間超過3秒就拋出異常
package com.java.learn;
import org.testng.annotations.Test;
/**
* create by Anthony on 2017/10/31
*/
public class TimeoutTest {
@Test(timeOut = 3000)
public void loginTest(){
try{
Thread.sleep(3100);
}catch (InterruptedException e){
System.out.println(e.toString());
}
}
}
運行下這個Testng測試用例,看是否拋出異常

更改下Thread.sleep(2800);再次運行,看看效果

屬性enabled
在Testng中,如果方法前面添加了@Test注釋,然后沒有其他的屬性,那么默認這個用例會被自動運行。在@Test注釋中提供了一個enabled的屬性,可以臨時去修改該條用例不被執(zhí)行。Enable屬性有兩個值true和false
不讓test2用例執(zhí)行
package com.java.learn;
import org.testng.annotations.Test;
/**
* create by Anthony on 2017/10/31
*/
public class TestNG_Demo2 {
@Test
public void test1(){
System.out.println("test1");
}
@Test(enabled=false)
public void test2(){
System.out.println("test2");
}
@Test
public void test3(){
System.out.println("test3");
}
@Test
public void test4(){
System.out.println("test3");
}
}
屬性priority
一個類文件下的測試用例按照我們設(shè)想的順序去執(zhí)行,而不是默認按照方法名的字母排序去執(zhí)行
@Test注釋中有一個屬性,叫priority支持設(shè)置用例的優(yōu)先級。如果不帶這個屬性,默認priority是等于0,而且priority值越小,優(yōu)先級越高
例子:設(shè)置的優(yōu)先順序是test4->test3->test1->test2
package com.java.learn;
import org.testng.annotations.Test;
/**
* create by Anthony on 2017/10/31
*/
public class TestNG_Demo2 {
@Test(priority = 2)
public void test1(){
System.out.println("test1");
}
@Test(priority = 4)
public void test2(){
System.out.println("test2");
}
@Test(priority = 1)
public void test3(){
System.out.println("test3");
}
@Test
public void test4(){
System.out.println("test4");
}
}
屬性invocationCount和invocationTimeOut
屬性和timOut差不多,只不過是監(jiān)控運行多次同一個用例的總耗時是否達到設(shè)置的最大值。
- invocationCount
字面意思就是調(diào)用的次數(shù)統(tǒng)計,這個屬性是的英文解釋是:The number of times this method should be invoked. 就是這個測試用例被調(diào)用執(zhí)行的次數(shù)。說明這個屬性可以設(shè)置一個用例可以重復(fù)跑多次。
- invocationTimeOut
字面意思是調(diào)用的超時,看看英文的解釋:The maximum number of milliseconds this test should take for thecumulated time of all the invocationcounts. This attribute will be ignored ifinvocationCount is not specified.這句話,說了兩個意思,第一個是設(shè)置一個最大的毫秒數(shù)來計算執(zhí)行多次后總共耗時多少,耗時值不能超過設(shè)置的最大毫秒數(shù)。第二個意思是說,這個屬性是和invocationCount結(jié)合使用才會工作
代碼舉例:設(shè)置了執(zhí)行次數(shù)數(shù)5次,5次執(zhí)行總共耗時不能超過5100毫秒,否則拋出中斷異常
package com.java.learn;
import org.testng.annotations.Test;
/**
* create by Anthony on 2017/10/31
*/
public class TimeoutTest {
@Test(invocationCount = 5, invocationTimeOut = 5100)
public void loginTest() throws InterruptedException{
Thread.sleep(1000);
System.out.println("login test");
}