1.異常測試
package com.testngdemo;
import org.testng.annotations.Test;
public class test {
@Test(expectedExceptions = ArithmeticException.class )
public void divisionWithException() {
int i = 1 / 0;
System.out.println("After division the value of i is :"+ i);
}
}
testng.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
<test name="test1">
<classes>
<class name="com.testngdemo.test"/>
</classes>
</test>
</suite>
運行結果:
2.忽略測試
package com.testngdemo;
import org.testng.annotations.Test;
public class test {
@Test
public void test1() {
System.out.println("這是第一個測試用例");
}
@Test(enabled = false )
public void test2(){
System.out.println("這是第二個測試用例");
}
}
testng.xml配置和異常測試一樣;
運行結果:
3.超時測試
package com.testngdemo;
import org.testng.annotations.Test;
public class test {
@Test
public void test1() {
System.out.println("這是第一個測試用例");
}
@Test(timeOut = 2000 )
public void test2()throws Exception{
Thread.sleep(3000);
System.out.println("這是第二個測試用例");
}
}
testng.xml配置和異常測試一樣;
運行結果:
4.優先級測試
使用@Test的priority屬性可支持設置用例的優先級。如果不帶這個屬性,默認priority是等於0,而且priority值越小,優先級越高;
package com.testngdemo;
import org.testng.annotations.Test;
public class test {
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");
}
}
}
testng.xml配置和異常測試一樣;
運行結果:
5.分組測試
package com.testngdemo;
import org.testng.annotations.Test;
public class test {
public class TestNG_Demo2 {
@Test(groups = {"Fucntion","API"})
public void test01(){
System.out.println("API Testing and Function testing");
}
@Test(groups = {"API"})
public void test02(){
System.out.println("API Testing");
}
@Test(groups = {"Fucntion"})
public void test03(){
System.out.println("Function testing");
}
@Test
public void test04(){
System.out.println("not in API and Function testing");
}
}
}
通過testng.xml配置只測試包含API的分組:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
<test name="test1">
<groups>
<run>
<include name="API "/>
</run>
</groups>
<classes>
<class name="com.testngdemo.test"/>
</classes>
</test>
</suite>
運行結果如下:
通過testng.xml配置只測試API和Fucntion的分組:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
<test name="test1">
<groups>
<run>
<include name="API "/>
<include name="Fucntion "/>
</run>
</groups>
<classes>
<class name="com.testngdemo.test"/>
</classes>
</test>
</suite>
運行結果如下:
通過testng.xml配置測試不含API和Fucntion的分組:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
<test name="test1">
<groups>
<run>
<exclude name="API"/>
<exclude name="Fucntion"/>
</run>
</groups>
<classes>
<class name="com.testngdemo.test"/>
</classes>
</test>
</suite>
運行結果:
通過testng.xml配置通過自定義分組管理API和Fucntion的分組:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
<test name="test1">
<groups>
<define name="all">
<include name="API"/>
<include name="Fucntion"/>
</define>
<run>
<include name="all"/>
</run>
</groups>
<classes>
<class name="com.testngdemo.test"/>
</classes>
</test>
</suite>
運行結果:
6.依賴測試
硬依賴:所有的依賴方法必須成功執行,纔會執行接下來的方法,如果其中有一個依賴的方法失敗了,那麼接下來的方法是不會被執行,會默認標記跳過(skip)。
package com.testngdemo;
import org.testng.annotations.Test;
public class test {
public class TestNG_Demo2 {
@Test(dependsOnMethods = "tomcatServiceIsDown")
public void restartTomcatService(){
System.out.println("Restart the tomcat server when it is down!");
}
@Test
public void tomcatServiceIsDown(){
System.out.println("tomcat service is down!");
}
}
}
軟依賴:不管依賴的方法是否成功執行,接下來的方法都可以運行。軟依賴的實現是在@Test中添加屬性alwaysRun=true來實現。
package com.testngdemo;
import org.testng.Assert;
import org.testng.annotations.Test;
public class test {
public class TestNG_Demo2 {
@Test(groups = {"tomcat"})
public void restartTomcatService(){
System.out.println("Restart the tomcat server when it is down!");
}
@Test(groups = {"tomcat"})
public void tomcatServiceIsDown(){
System.out.println("tomcat service is down!");
Assert.assertTrue(10==11);
}
@Test(dependsOnGroups = {"tomcat"}, alwaysRun = true)
public void startAppServer(){
System.out.println("Start App service");
}
}
}
通過testng.xml配置依賴暫忽略;
7.附@Test註解相關屬性
編號 |
屬性名稱 |
屬性作用 |
備註 |
1 |
alwaysRun |
如果設置爲true,則此測試方法將始終運行,即使它取決於失敗的方法。 |
|
2 |
dataProvider |
該測試方法的數據提供者的名稱。 |
|
3 |
dataProviderClass |
在哪裏尋找數據提供者的類。 |
|
4 |
dependsOnGroups |
此方法所依賴的組列表。 |
|
5 |
dependsOnMethods |
此方法所依賴的方法列表。 |
|
6 |
description |
此方法的說明。 |
|
7 |
enabled |
是否啓用了此類/方法上的方法。 |
|
8 |
expectedExceptions |
預期測試方法將引發的異常列表。 |
|
9 |
expectedExceptionsMessageRegExp |
如果指定了ExpectedExceptions,則其消息必須與在此屬性中指定的正則表達式匹配。 |
|
10 |
groups |
此類/方法所屬的組的列表。 |
|
11 |
ignoreMissingDependencies |
如果設置爲true,則即使缺少或排除了它依賴的方法,該測試也將運行。 |
|
12 |
invocationCount |
應該調用此方法的次數。 |
|
13 |
invocationTimeOut |
此測試方法的調用總數應採用的最大毫秒數。 |
|
14 |
parameters |
不推薦使用 |
建議使用Use @Parameters |
15 |
priority |
調度優先級。 |
|
16 |
retryAnalyzer |
如果應重試測試,應調用該類的名稱以進行測試。 |
|
17 |
sequential |
不推薦使用 |
建議使用單線程 |
18 |
singleThreaded |
如果設置爲true,則即使當前正在使用parallel =“ true”運行測試,也保證該測試類上的所有方法都可以在同一線程中運行。 |
|
19 |
skipFailedInvocations |
如果將true和invocationCount指定爲爲1,則失敗後的所有調用都將被標記爲SKIP而不是FAIL。 |
|
20 |
successPercentage |
此方法預期成功的百分比。 |
|
21 |
suiteName |
該測試類應放入的套件名稱。 |
|
22 |
testName |
應該放置該測試類的測試的名稱 |
|
23 |
threadPoolSize |
此方法的線程池的大小。 |
|
24 |
timeOut |
此測試應花費的最大毫秒數。 |
|