<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Java接口自動化之TestNG單元測試框架(一)

          共 4269字,需瀏覽 9分鐘

           ·

          2021-02-02 08:36














          本文3704字

          閱讀約需10分鐘

          第203次推送




          上一篇Java接口自動化系列文章:Java接口自動化之log4j日志框架,主要介紹log4j日志介紹、日志三大組成部分及日志實戰(zhàn)。


          以下主要介紹TestNG的簡介、@Test注解及其屬性。



          01
          TestNG簡介

          1 TestNG介紹

          TestNG 是java的一個單元測試框架,TestNG吸取了Junit框架的思想,形成了更強大的集成測試框架。

          2 TestNG特點

          • 支持注解;

          • 靈活的運行配置;

          • 支持多線程、忽略、異常、參數(shù)化等測試。


          3 加入TestNG依賴

          在maven項目的pom.xml中,添加內(nèi)容如下:


          ????org.testng
          ????testng
          ????7.0.0


          02
          @Test注解及常用屬性

          @@Test是TestNG 最基本的注解,用來將方法標注為測試方法。接下來會介紹@Test注解的常用屬性

          @Test是TestNG 最基本的注解,用來將方法標注為測試方法。接下來會介紹@Test注解的常用屬性。


          1 enable 測試方法是否執(zhí)行

          enable默認是?true?, 表示執(zhí)行這個方法,如果設置為?false?,則在運行時不會執(zhí)行這個測試方法。

          import?org.testng.annotations.Test;
          public?class?TestDemo?{
          ????@Test(enabled?=?true)
          ????public??void?testDemo1(){
          ????????System.out.println("這是testDemo1");
          ????}
          ????@Test(enabled?=?false)
          ????public??void?testDemo2(){
          ????????System.out.println("這是testDemo2");
          ????}
          }


          運行結果為:

          從上圖可以看出,enabled為true的方法執(zhí)行了,enabled為false的方法未執(zhí)行。

          2 dependsOnMethods 依賴方法

          在依賴的方法運行完成之后運行當前方法,如果依賴方法測試不通過,那么當前方法也不會繼續(xù)運行了。

          依賴的方法可以有多個,格式為:@Test(dependsOnMethods = { "method1" , “method2” })。

          修改testDemo1代碼,運行時拋出異常,代碼如下:

          import?org.testng.annotations.Test;
          public?class?TestDemo?{
          ????@Test()
          ????public??void?testDemo1(){
          ????????int?i=10;
          ????????System.out.println(i/0);
          ????????System.out.println("這是testDemo1");
          ????}
          ????@Test(dependsOnMethods?=?{"testDemo1"})
          ????public??void?testDemo2(){
          ????????System.out.println("這是testDemo2");
          ????}
          }


          運行結果為:

          從上圖可以看出testDemo1方法運行失敗后,testDemo2方法被忽略運行了。


          3 groups 分組

          在運行時,一個組的方法會一起運行,然后再運行下一個組的方法;

          格式為:@Test(groups = "groupName")。

          import?org.testng.annotations.Test;
          import?org.testng.annotations.BeforeGroups;
          import?org.testng.annotations.AfterGroups;
          public?class?TestDemo?{
          ????@BeforeGroups(groups?=?"server")
          ????public?void?beforeGroupsOnServer()?{
          ????????System.out.println("服務端組運行前執(zhí)行的方法。。。");
          ????}
          ????@BeforeGroups(groups?=?"client")
          ????public?void?beforeGroupsOnClient()?{
          ????????System.out.println("客戶端組運行前執(zhí)行的方法。。。");
          ????}
          ????@Test(groups?=?"server")
          ????public?void?test1()?{
          ????????System.out.println("server?test1?run?....");
          ????}
          ????@Test(groups?=?"server")
          ????public?void?test2()?{
          ????????System.out.println("server?test2?run?....");
          ????}
          ????@Test(groups?=?"client")
          ????public?void?test3()?{
          ????????System.out.println("client?test3?run?....");
          ????}
          ????@Test(groups?=?"client")
          ????public?void?test4()?{
          ????????System.out.println("client?test4?run?....");
          ????}
          ????@AfterGroups(groups?=?"server")
          ????public?void?afterGroupsOnServer()?{
          ????????System.out.println("服務端組運行之后執(zhí)行的方法。。。");
          ????}
          ????@AfterGroups(groups?=?"client")
          ????public?void?afterGroupsOnClient()?{
          ????????System.out.println("客戶端組運行之后執(zhí)行的方法。。。");
          ????}
          }

          運行結果為:

          服務端組運行前執(zhí)行的方法。。。
          server?test1?run?....
          server?test2?run?....
          服務端組運行之后執(zhí)行的方法。。。
          客戶端組運行前執(zhí)行的方法。。。
          client?test3?run?....
          client?test4?run?....
          客戶端組運行之后執(zhí)行的方法。。。
          ===============================================
          Default?Suite
          Total?tests?run:?4,?Failures:?0,?Skips:?0



          4?timeOut 超時屬性

          格式:@Test(timeOut = 3000) 設置超時時間,單位為毫秒。


          import?org.testng.annotations.Test;
          public?class?TestDemo?{
          ????//?單位為毫秒值,3秒內(nèi)沒有響應,就運行失敗,反之成功
          ????@Test(timeOut?=?3000)
          ????public?void?testSuccess()?throws?InterruptedException?{
          ????????Thread.sleep(2000);
          ????}
          ????//?單位為毫秒值,2秒內(nèi)沒有響應,就運行失敗,反之成功
          ????@Test(timeOut?=?2000)
          ????public?void?testFail()?throws?InterruptedException?{
          ????????Thread.sleep(3000);
          ????}
          }


          運行結果為:

          從上圖可以看出,testFail方法因為超時運行失敗。


          5 多線程測試

          格式:@Test(invocationCount = 10,threadPoolSize = 3)。

          參數(shù)說明:

          invocationCount:線程調(diào)用的次數(shù),默認1次。

          threadPoolSize:線程池,需與invocationCount組合使用。


          接下來編寫一個方法,定義3個線程,執(zhí)行方法10次。

          import?org.testng.annotations.Test;
          public?class?TestDemo?{
          ????@Test(invocationCount?=?10,threadPoolSize?=?3)
          ????public?void?testDemo(){
          ????????System.out.println("---------");
          ????????System.out.printf("Thread?Id?:?%s%n",Thread.currentThread().getId());
          ????}
          }


          運行結果為:

          [TestNG]?Running:
          ?
          ---------
          ---------
          Thread?Id?:?13
          ---------
          Thread?Id?:?12
          Thread?Id?:?14
          ---------
          Thread?Id?:?13
          ---------
          Thread?Id?:?14
          ---------
          Thread?Id?:?12
          ????
          ---------
          Thread?Id?:?12
          ---------
          Thread?Id?:?13
          ---------
          Thread?Id?:?14
          ---------
          Thread?Id?:?12
          ===============================================
          Default?Suite
          Total?tests?run:?10,?Failures:?0,?Skips:?0
          ===============================================



          (完)




          ITester測試開發(fā)小棧(ID:TestDevZone),技術人茶余飯后的充電寶,每周上午11:30更新文章。從精進的軟件測試到硬核的測試開發(fā)、從精細的前端開發(fā)到縝密的后端開發(fā),前沿互聯(lián)網(wǎng)資訊傳送,寶藏IT資源分享,熱門招聘發(fā)布。

          喜歡記得星標置頂,讓我們一起守護成長

          瀏覽 155
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产69精品久久久久久久久久久久 | 亚洲黄色做爱 | 影音av资源 | 天天射夜夜操 | 国产操B视频 |