TestNG入門到...


 

目錄

一、概述

二、@Test注解常用參數

三、測試中常用的斷言(assert)

四、TestNG常用注解及使用

五、配置文件xml常用標簽

六、參數傳遞

七、測試報告

 

一、概述

1、TestNG是一個開源自動化測試框架,其靈感來自JUnit和NUnit,TestNG還涵蓋了整個核心的JUnit4功能,但引入了一些新的功能,使其功能更強大,使用更方便。

優勢:支持依賴測試方法,並行測試,負載測試,局部故障;靈活的插件API;支持多線程測試;

2、Maven依賴

       <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.7</version>
            <scope>test</scope>
        </dependency>

注:Eclicpe上想要直接運行還需要安裝testng插件

3、helloword

(1)被測試方法類HelloWrold:

package study.testng;

public class HelloWorld {

    public String hello(){
        return "hello world !";
    }
}

(2)測試類TestHelloWorld:

package study.testng;

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestHelloWorld {

    //測試返回結果不為空
    @Test
    public void tester1(){
        HelloWorld hello = new HelloWorld();
        String helloworld = hello.hello();

        Assert.assertNotNull(helloworld);
    }
    
    //測試返回結果為”hello world !“字符串
    @Test
    public void tester2(){
        HelloWorld hello = new HelloWorld();
        String helloworld = hello.hello();
        System.out.println(helloworld);

        Assert.assertEquals(helloworld, "hello world !");
    }
}

(3)運行測試

 

(4)測試結果

 

二、@Test注解常用參數

1、測試方法是否執行enable

默認是true,如果設置為false,則在運行時不會執行這個測試方法;

 

2、預期異常expectedExeption

@Test(expectedExceptions = ClassName.class)

如果ClassName類拋出了異常,測算測試通過,沒有異常算測試不通過;

expectedExceptions的值也可以是一個數組:

@Test(expectedExceptions = {ClassName.class, ClassName2.class,... })

 

3、超時timeOut

單位為毫秒,如果測試方法運行時間超這個值算測試不通過;

 

4、分組groups

(1)把在一個<test>標簽內的中所有類方法再進行組,在運行時,一個組的方法會一起運行,然后再運行下一個組的方法;

(2)分組的最小維度為方法,當把帶分組的@Test(groups = ”groupName”)注解對類使用時,這個測試類中的所有方法都屬於這同一個組;

(3)一個方法也可以同時屬於多個組,@Test(groups = {“groupName1”, “groupName2”}),那么每組運行時這個方法都會被執行一次;

(4)同一個組里的方法類,如果分別屬於兩個不同的測試用例(<test>)內,那么它們其實可以算兩個組,它們會在每個測試用例分別運行,而不會合在一起運行;

 

5、依賴方法dependsOnMethods

在被依賴的方法運行完成之后運行當前方法,如果依賴方法測試不通過,那么當前方法也不會繼續運行了;依賴的方法可以有多個;

例:@Test(dependsOnMethods = { "methodName1" , “methodName2” })

 

6、依賴組,dependsOnGroups

和依賴方法類似,在被依賴組運行完成之后運行當前組,如果依賴組中的方法沒有測試能過,那么當前的方法也不會繼續運行了;依賴組可以有多個;

 

三、測試中常用的斷言(assert)

1 assertEqual ([String message], expected value, actual value)        斷言兩個值相等。值可能是類型有 int, short, long, byte, char or java.lang.Object. 第一個參數是一個可選的字符串消息;
2 assertTrue([String message], boolean condition)                斷言一個條件為真;
3 assertFalse([String message],boolean condition)              斷言一個條件為假;
4 assertNotNull([String message], java.lang.Object object)           斷言一個對象不為空(null);
5 assertNull([String message], java.lang.Object object)            斷言一個對象為空(null);
6 assertSame([String message], java.lang.Object expected, java.lang.Object actual)       斷言兩個對象引用相同的對象;
7 assertNotSame([String message], java.lang.Object unexpected, java.lang.Object actual)    斷言兩個對象不是引用同一個對象;
8 assertArrayEquals([String message], expectedArray, resultArray)                  斷言預期數組和結果數組相等。數組的類型可能是 int, long, short, char, byte or java.lang.Object.;

 

四、TestNG常用注解及使用

@BeforeSuite     在該套件的所有測試都運行在注釋的方法之前,僅運行一次(套件測試是一起運行的多個測試類)。
@AfterSuite      在該套件的所有測試都運行在注釋方法之后,僅運行一次。
@BeforeClass     在調用當前類的第一個測試方法之前運行,注釋方法僅運行一次。
@AfterClass      在調用當前類的第一個測試方法之后運行,注釋方法僅運行一次
@BeforeTest      注釋的方法將在屬於<test>標簽內的類的所有測試方法運行之前運行。
@AfterTest       注釋的方法將在屬於<test>標簽內的類的所有測試方法運行之后運行。
@BeforeGroups    配置方法將在之前運行組列表。 此方法保證在調用屬於這些組中的任何一個的第一個測試方法之前不久運行。
@AfterGroups     此配置方法將在之后運行組列表。該方法保證在調用屬於任何這些組的最后一個測試方法之后不久運行。
@BeforeMethod    注釋方法將在每個測試方法之前運行。
@AfterMethod     注釋方法將在每個測試方法之后運行。
@Parameters      描述如何將參數傳遞給@Test方法。
@DataProvider    標記一種方法來提供測試方法的數據。 注釋方法必須返回一個Object [] [],其中每個Object []可以被分配給測試方法的參數列表。 要從該DataProvider接收數據的@Test方法需要使用與此注釋名稱相等的dataProvider名稱。
@Factory         將一個方法標記為工廠,返回TestNG將被用作測試類的對象。 該方法必須返回Object []。
@Listeners       定義測試類上的偵聽器。
@Test            將類或方法標記為測試的一部分。

例:

1、增加一個測試類TestConfig

package study.testng;

import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestConfig {

    @BeforeSuite
    public void beforeSuite() {
        System.out.println("測試套件(當前xml中<suite>標簽)之前運行@BeforeSuite--------------------");
    }

    @AfterSuite
    public void afterSuite() {
        System.out.println("測試套件(當前xml中<suite>標簽)之后運行@AfterSuite--------------------\n");
    }

    @BeforeTest
    public void beforeTest() {
        System.out.println("測試用例(當前xml中<test>標簽)之前運行@BeforeTest----------");
    }

    @AfterTest
    public void afterTest() {
        System.out.println("測試用例(當前xml中<test>標簽)之后運行@AfterTest----------\n");
    }
    
    @BeforeMethod
    public void beforeMethod() {
        System.out.println("當前類每個測試方法(@Test)之前運行@BeforeMethod");
    }
    
    @AfterMethod
    public void AfterMethod(){
        System.out.println("當前類每個測試方法(@Test)之后運行@AfterMethod");
    }
    
    @BeforeGroups(value="group1")
    public void beforeGroups(){
        System.out.println("配置組配group1之前運行@BeforeGroups..................");
    }
    @AfterGroups(value="group1")
    public void afterGroups(){
        System.out.println("配置組配group1之前運行@AfterGroups..................");
    }
    
    @Test
    public void test1(){
        System.out.println("runnig TestConfig.test1()");
    }
    
    @Test(groups = "group1")
    public void test2(){
        System.out.println("runnig TestConfig.test2()");
    }
    
    @Test(groups = "group1")
    public void test3(){
        System.out.println("runnig TestConfig.test3()");
    }
    
}
View Code

2、新建一個自定義xml配置文件tester.xml(位置在哪都行)

<?xml version="1.0" encoding="UTF-8"?>

<!-- @BeforeSuite -->
<suite name="tester">

    <!-- @BeforeTest -->
    <test name="case1">
      <classes>
        <class name="study.testng.TestConfig" />
        <class name="study.testng.TestHelloWorld" />
      </classes>
    </test>
    <!-- @AfterTest -->
    
    <!-- @BeforeTest -->
    <test name="case2">
      <classes>
        <class name="study.testng.TestConfig" />
      </classes>
    </test>
    <!-- @AfterTest -->

</suite>
<!-- @AfterSuite -->
View Code

3、運行此配置測試

在有@Test方法的類頁面右鍵

選擇xml文件

4、運行結果

從這個結果顯示出注釋的作用位置。其中@BeforeGroups和@AfterGroups的作用范圍是可以跨類的,但類必須是在同一個測試用例(<test>標簽)范圍內;

這些標簽的運行先后順序可以總結為:

@BeforeSuite->@BeforeTest->@BeforeClass->{@BeforeMethod->@Test->@AfterMethod}->@AfterClass->@AfterTest->@AfterSuite(其中{}內的與多少個@Test,就循環執行多少次)。

 

五、配置文件xml常用標簽

<suite>  套件,根標簽,通常由幾個<test組成>
  屬性:
  name            套件的名稱,必須屬性;
  verbose         運行的級別或詳細程度;
  parallel        是否運行多線程來運行這個套件;
  thread-count    如果啟用多線程,用於指定開戶的線程數;
  annotations     在測試中使用的注釋類型;
  time-out        在本測試中的所有測試方法上使用的默認超時時間; 
<test>    測試用例,name為必須屬性;
<classes>  用例中包含的類,子標簽為<class name=”className”>;
<class>    測試類,其中屬性name為必須屬性;;
<packages> 用例中包含的包,包中所有的方法都會執行,子標簽為<package name=”packageName”>;
<package>  測試包,name為必須屬性;
<methods>  指定測試類中包含或排除的方法,子類為<include>,<exclude>;
<include>  指定需要測試的方法,name為必須屬性;
<exclude>  指定類中不需要測試的方法,name為必須屬性;
<groups>   指定測試用例中要運行或排除運行的分組,子標簽為<run>,<run>下包含<include>,<exclude>標簽,<include>,<exclude>的name指定運行、不運行的分組;

例:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="tester">

    <test name="case1">
      <classes>
        <class name="study.testng.TestHelloWorld"/>
        <class name="study.testng.TestConfig">    
            <methods>
                <include name="test1" />   <!-- 運行test1()方法-->
                <exclude name="test2" />   <!-- 不運行test2()方法-->
            </methods>
        </class>    
      </classes>
    </test>

    <test name="case2">
      <packages>
        <package name="study.testng" />
      </packages>
      <groups>
        <run>
            <exclude name="group1" />   <!-- 不運行組group1的所有方法,或者也可include標簽來表示需要行的組-->
        </run>
      </groups>
    </test>

</suite>
View Code

六、參數傳遞

1、使用@Parameters注解從測試配置xml文件獲取參數

(1)新建測試類TestParameter

package study.testng;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class TestParameter {
    
    @Test
    @Parameters({"param", "param2"})
    public void printParameters(String param, String param2){
        System.out.println("param參數值為 : " + param);
        System.out.println("param2參數值為 : " + param2);
    }
}

(2)新建配置xml文件,parameterTeser.xml

<?xml version="1.0" encoding="UTF-8"?>

<suite name="parameterTester">
    <test name="case1">
        <parameter name="param"  value="param的值"/>
        <parameter name="param2"  value="param2的值"/>
        <classes>
            <class name="study.testng.TestParameter" />
        </classes>
    </test>
</suite>

(3)以這個配置文件運行測試

(4)結果

2、使用@DataProvider傳送參數,@DataProvider可以傳遞一些比較復雜的參數

(1)新建一個測試類TestParameter2

package study.testng;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestParameter2  {
    @DataProvider(name="user")
    public Object[][] Users(){
        return new Object[][]{
                {"root","passowrd"},
                {"cnblogs.com", "tankxiao"},
                {"tank","xiao"}
        };
    }
    @Test(dataProvider="user")
    public void verifyUser(String userName, String password){
        System.out.println("Username: "+ userName + " Password: "+ password);
    }
}

(2)點擊運行得到如下結果

 

七、測試報告

默認的測試報告位於當前項目文件夾下的test-output文件夾內,index.html即為總的測試報告(html文件用瀏覽器打開),tester文件夾下是按測試用例生成的報告,old文件夾下為歷史報告。

 

 

附:與Junit4的異同比較,可直接參考使用Junit4;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM