@Test 注解
順序如下:
beforeSuite?
beforeClass
beforeMethod
這是@test
afterMethod
afterClass
afterSuite
忽略測試
@Test(enabled = false)? ?忽略
@Test(enabled = true)? ? 執(zhí)行
組測試
groups on method
groups on class
package com.course.testng.groups;
import org.testng.annotations.Test;
public class GroupsOnMethod {
? @Test(groups = "jane")
? ? public void method1(){
? ? ? ? System.out.println("這是jane的method111");
? ? }
? ? @Test(groups = "jane")
? ? public void method2(){
? ? ? ? System.out.println("這是jane的merhod222");
? ? }
? ? @Test(groups = "group")
? ? public void method3(){
? ? ? ? System.out.println("這是groups的method333");
? ? }
}
創(chuàng)建一個文件 testng.xml?C:\ > TestNG_WORKSPACE?來執(zhí)行測試用例,在這里,我們將只執(zhí)行這些測試,屬于組functest。
<?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="jane" />
? ? </run>
</groups>
<classes>
? ? <class name="GroupsOnMethod?" />
</classes>
? ? </test>
</suite>
異常測試
@Test(expectedExceptions = ArithmeticException.class)
public void divisionWithException() {
? ? int i = 1 / 0;
? ? System.out.println("After division the value of i is :"+ i);
}
依賴測試
TestNG允許指定依賴關(guān)系:
在@Test注釋中使用屬性dependsOnMethods,或者
在@Test注釋中使用屬性dependsOnGroups。
// This test will be failed.
@Test
public void method1() {
System.out.println("This is method 1");
throw new RuntimeException();
}
@Test(dependsOnMethods = { "method1" })
public void method2() {
System.out.println("This is method 2");
}
依賴測試
TestNG允許指定依賴關(guān)系:
在@Test注釋中使用屬性dependsOnMethods,或者
在@Test注釋中使用屬性dependsOnGroups。
// This test will be failed.
@Test
public void method1() {
System.out.println("This is method 1");
throw new RuntimeException();
}
@Test(dependsOnMethods = { "method1" })
public void method2() {
System.out.println("This is method 2");
}
參數(shù)化
常用的有@Parameters和@DataProvider兩種注解。
TestNG可以通過兩種不同的方式將參數(shù)直接傳遞給測試方法:
使用testng.xml
使用數(shù)據(jù)提供者
通過XML@Parameters或@DataProvider將參數(shù)傳遞給@Test方法。
可以通過@Optional來指定默認(rèn)值,如果testng.xml沒有配置對應(yīng)的參數(shù),則使用默認(rèn)值傳參。
@Parameters
public class ParametersOnXML {
? ? @Test
? ? @Parameters(value = "para")
? ? public void parameterstest1( String a){
? ? ? ? System.out.println("參數(shù)值為"+a);
? ? }
? ? @Test
? ? @Parameters(value = "para1")? ?
? ? public void parameterstest2(@Optional("0") String b){
? ? ? ? System.out.println("默認(rèn)參數(shù)為"+b);
? ? }
}
運(yùn)行以下xml文件即可
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="parameters">
<test name="parameters">
? <parameter name="para" value="999"></parameter>
? ? <classes>
? ? ? ? <class name="com.course.testng.parameters.ParametersOnXML" />
? ? </classes>
</test>
</suite>
@DataProvider
注解的方法返回對象數(shù)組
public class DataProviderTest {
? ? @Test(dataProvider = "provider")
? ? public void testDataProvider(String a,int b){
? ? ? ? System.out.println("參數(shù)="+a+";數(shù)字="+b);
? ? }
? ? @DataProvider(name = "provider")
? ? public Object[] [] providerData(){
? ? ? ? Object[] [] o = new Object[][]{
? ? ? ? ? ? ? ? {"a",1},
? ? ? ? ? ? ? ? {"b",2},
? ? ? ? };
? ? ? ? return o;
? ? }
}
根據(jù)方法 進(jìn)行參數(shù)傳遞,test1和testt2
@DataProvider(name = "methodData")
public Object[][] methodDataTest(Method method){
? ? Object[][] result=null;
? ? if(method.getName().equals("test1")) {
? ? ? ? result = new Object[][]{
? ? ? ? ? ? ? ? {"aa", 11},
? ? ? ? ? ? ? ? {"bb", 22}
? ? ? ? };
? ? }else if (method.getName().equals("test2")){
? ? ? ? ? ? result=new Object[][]{
? ? ? ? ? ? ? ? ? ? {"CC",33},
? ? ? ? ? ? ? ? ? ? {"dd",44}
? ? ? ? ? ? };
? ? }
? ? return result;
? ? ? ? }
? ? }
多線程
@注解方式
在測試方法中,指定其可用的線程池
public class MultiThread {
//? ? 測試方法在3個線程中并發(fā)執(zhí)行,共被調(diào)用5次,執(zhí)行超過10s
? ? @Test(threadPoolSize = 3,invocationCount = 5,timeOut = 10000)
? ? public void multiThreadTest(){
? ? ? ? System.out.printf("test:%s %n",Thread.currentThread().getId());
? ? }
}
@XML方式
public class MultiThreadOnXML {
? ? @Test
? ? public void test1() {
? ? ? ? System.out.printf("test1 的線程:%s%n? ",? Thread.currentThread().getId() );
? ? }
? ? @Test
? ? public void test2() {
? ? ? ? System.out.printf("test2 的線程:%s%n? ",? Thread.currentThread().getId() );
? ? }
? ? @Test
? ? public void test3() {
? ? ? ? System.out.printf("test3? 的線程:%s%n? ",? Thread.currentThread().getId() );
? ? }
}
XML文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="threads" parallel="methods" thread-count="3">
<!--
tests級別:不同test tag下的用例可以在不同的線程執(zhí)行,相同test tag下的用例只能在同一個線程中執(zhí)行。
classs級別:不同class tag下的用例可以在不同的線程執(zhí)行,相同class tag下的用例只能在同一個線程中執(zhí)行。
methods級別:所有用例都可以在不同的線程去執(zhí)行。
一般methods級別就可以
? ? -->
? ? <test name="test1">
? ? ? ? <classes>
? ? ? ? ? ? <class name="com.course.testng.multiThread.MultiThreadOnXML"></class>
? ? ? ? ? ? <class name="com.course.testng.BasicAnnotation"></class>
? ? ? ? </classes>
? ? </test>
? ? <test name="test2">
? ? ? ? <classes>
? ? ? ? ? ? <class name="com.course.testng.multiThread.MultiThreadOnXML"></class>
? ? ? ? </classes>
? ? </test>
</suite>
一般情況下,一個testng.xml只包含一個suite。如果想起多個線程執(zhí)行不同的suite,官方給出的方法是:通過命令行的方式來指定線程池的容量。
java org.testng.TestNG -suitethreadpoolsize 3 testng1.xml testng2.xml testng3.xml
超時(shí)測試
public class TimeOutTest {
? ? @Test(timeOut = 3000)
? ? public void testSucess() throws InterruptedException{
? ? ? ? Thread.sleep(2000);
? ? }
? ? @Test(timeOut = 2000)
? ? public void testFailed() throws InterruptedException{
? ? ? ? Thread.sleep(3000);
? ? }
}