Testng測試框架

@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);

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,181評論 0 3
  • 總結(jié) 加dependency依賴(獲取注解和注解處理器) 安裝myeclipse插件(調(diào)用注解處理器) 添加注解(...
    果芽軟件閱讀 702評論 0 2
  • JAVA面試題 1、作用域public,private,protected,以及不寫時(shí)的區(qū)別答:區(qū)別如下:作用域 ...
    JA尐白閱讀 1,262評論 1 0
  • 若不是家里人是此次活動的組織者,也不會有這幾天的體驗(yàn)。 自記事起就沒有人能夠組織起這樣的活動來,其中的原因太多了!...
    解凡人奔波離散苦閱讀 443評論 0 0
  • 文/無尾魚 筆友叫我寫同題《阿貴》, 太難!東南西北,四面八方,叫阿貴的太多, 阿貓阿狗,人生百態(tài),關(guān)于阿貴的就更...
    Z無尾魚閱讀 320評論 0 0

友情鏈接更多精彩內(nèi)容