@Test 注解
順序如下:
beforeSuite?
beforeClass
beforeMethod
這是@test
afterMethod
afterClass
afterSuite
忽略測(cè)試
@Test(enabled = false)? ?忽略
@Test(enabled = true)? ? 執(zhí)行
組測(cè)試
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)建一個(gè)文件 testng.xml?C:\ > TestNG_WORKSPACE?來(lái)執(zhí)行測(cè)試用例,在這里穗熬,我們將只執(zhí)行這些測(cè)試横侦,屬于組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>
異常測(cè)試
@Test(expectedExceptions = ArithmeticException.class)
public void divisionWithException() {
? ? int i = 1 / 0;
? ? System.out.println("After division the value of i is :"+ i);
}
依賴測(cè)試
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");
}
依賴測(cè)試
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ù)直接傳遞給測(cè)試方法:
使用testng.xml
使用數(shù)據(jù)提供者
通過XML@Parameters或@DataProvider將參數(shù)傳遞給@Test方法捐顷。
可以通過@Optional來(lái)指定默認(rèn)值荡陷,如果testng.xml沒有配置對(duì)應(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
注解的方法返回對(duì)象數(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;
? ? ? ? }
? ? }
多線程
@注解方式
在測(cè)試方法中叮姑,指定其可用的線程池
public class MultiThread {
//? ? 測(cè)試方法在3個(gè)線程中并發(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級(jí)別:不同test tag下的用例可以在不同的線程執(zhí)行传透,相同test tag下的用例只能在同一個(gè)線程中執(zhí)行耘沼。
classs級(jí)別:不同class tag下的用例可以在不同的線程執(zhí)行,相同class tag下的用例只能在同一個(gè)線程中執(zhí)行朱盐。
methods級(jí)別:所有用例都可以在不同的線程去執(zhí)行群嗤。
一般methods級(jí)別就可以
? ? -->
? ? <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>
一般情況下,一個(gè)testng.xml只包含一個(gè)suite托享。如果想起多個(gè)線程執(zhí)行不同的suite骚烧,官方給出的方法是:通過命令行的方式來(lái)指定線程池的容量。
java org.testng.TestNG -suitethreadpoolsize 3 testng1.xml testng2.xml testng3.xml
超時(shí)測(cè)試
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);
? ? }
}