自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

測試驅(qū)動(dòng)技術(shù)(TDD)系列之2:詳解TestNG參數(shù)化

開發(fā) 前端
本篇文章只重點(diǎn)講解TestNG參數(shù)化相關(guān)知識(shí),

[[376877]]

 上一篇文章介紹了測試驅(qū)動(dòng)的相關(guān)概念,并以junit4為例,帶大家了解如何在測試框架中實(shí)現(xiàn)測試驅(qū)動(dòng)。詳情:

測試驅(qū)動(dòng)技術(shù)系列之1:一文帶你上手測試數(shù)據(jù)驅(qū)動(dòng)

大家會(huì)發(fā)現(xiàn)Junit4在同一個(gè)測試類中實(shí)現(xiàn)多組數(shù)據(jù)的數(shù)據(jù)驅(qū)動(dòng),不是很方便,需要我們自己解決!我也說過在TestNG中這個(gè)問題很容易搞定!給自己挖了個(gè)坑!自己挖的坑總要填上,那么今天就給大家講解一下數(shù)據(jù)驅(qū)動(dòng)在TestNG中的應(yīng)用。本篇文章只重點(diǎn)講解TestNG參數(shù)化相關(guān)知識(shí),關(guān)于TestNG的環(huán)境配置以及基礎(chǔ)使用不在本文的討論范圍!TestNG中使用DataProvider實(shí)現(xiàn)參數(shù)化功能,在執(zhí)行用例的時(shí)候dataProvider迭代中的每組數(shù)據(jù)都是作為一個(gè)用例執(zhí)行。

一組參數(shù)化數(shù)據(jù)

定義參數(shù)化數(shù)據(jù),代碼如下:

  1. @DataProvider 
  2.       public Object[][] dp1() { 
  3.         return new Object[][] { 
  4.           new Object[] { 1, 1,0 }, 
  5.           new Object[] { 2, 1,1 }, 
  6.           new Object[] { 2, 1,2 }, 
  7.         }; 
  8.       } 

 用例中使用參數(shù)化數(shù)據(jù)(dp1定義的),代碼如下:

  1. @Test(dataProvider = "dp1"
  2.   public void f1(Integer n1, Integer n2, Integer n3) { 
  3.      Integer result=n2+n3; 
  4.      assertEquals(n1,result); 
  5.   } 

 多組參數(shù)化數(shù)據(jù)

在一個(gè)測試類中,可以定義多組參數(shù)化數(shù)據(jù)(參數(shù)化數(shù)據(jù)個(gè)數(shù)不同,dp1三個(gè),dp2二個(gè)),代碼如下:

  1. @DataProvider 
  2.      public Object[][] dp2() { 
  3.        return new Object[][] { 
  4.          new Object[] { 1, 1}, 
  5.          new Object[] { 2, 1}, 
  6.          new Object[] { 2, 1}, 
  7.        }; 
  8.      } 

 用例中使用參數(shù)化數(shù)據(jù)(dp2定義的),代碼如下:

  1. @Test(dataProvider = "dp2"
  2.   public void f2(Integer n1, Integer n2) { 
  3.        assertEquals(n1,n2); 
  4.   } 

 測試方法通過DataProvider引用

我們可以自定義一些方法,對外部文件進(jìn)行讀取,然后把讀取的數(shù)據(jù)作為參數(shù)在TestNG中引用,具體方法如下:

寫一個(gè)讀取各類文件的方法(txt、excel、db)具體代碼就不給出了,但是一定注意方法的返回值必須是Object[][](關(guān)于操控excel的知識(shí)會(huì)在后面的文章中介紹)

  1. public Object[][] readfile(String p_file){ 
  2.          return  new Object[][] {{ 1, 1},{ 1, 2}}; 
  3.      } 

 在@DataProvider修飾的方法中調(diào)用該方法(也可以直接實(shí)現(xiàn))

  1. @DataProvider(name = "dp_func"
  2.        public Object[][] testData(Method testMethod) { 
  3.          return readfile(""); 
  4.        } 

 用例中使用參數(shù)化數(shù)據(jù)(dp_func),代碼如下: 

  1. @Test(dataProvider = "dp_func"
  2.   public void f3(Integer n1, Integer n2) { 
  3.         assertEquals(n1,n2); 
  4.   } 

 完整代碼 

  1. import org.testng.annotations.DataProvider; 
  2. import org.testng.annotations.Test; 
  3. import static org.testng.Assert.assertEquals; 
  4. import java.lang.reflect.Method; 
  5. public class NewTest { 
  6.  
  7.       @DataProvider 
  8.       public Object[][] dp1() { 
  9.         return new Object[][] { 
  10.           new Object[] { 1, 1,0 }, 
  11.           new Object[] { 2, 1,1 }, 
  12.           new Object[] { 2, 1,2 }, 
  13.         }; 
  14.       } 
  15.  
  16.       @DataProvider 
  17.       public Object[][] dp2() { 
  18.  
  19.         return new Object[][] { 
  20.           new Object[] { 1, 1}, 
  21.           new Object[] { 2, 1}, 
  22.           new Object[] { 2, 1}, 
  23.         }; 
  24.       } 
  25.  
  26.       @DataProvider(name = "dp_func"
  27.         public Object[][] testData(Method testMethod) { 
  28.           return readfile(""); 
  29.         } 
  30.  
  31.      //讀取各類文件,返回值是Object[][] 
  32.  
  33.       public Object[][] readfile(String p_file){ 
  34.           return  new Object[][] {{ 1, 1},{ 1, 2}}; 
  35.       } 
  36.  
  37.   @Test(dataProvider = "dp1"
  38.   public void f1(Integer n1, Integer n2, Integer n3) { 
  39.       Integer result=n2+n3; 
  40.       assertEquals(n1,result); 
  41.   } 
  42.  
  43.   @Test(dataProvider = "dp2"
  44.   public void f2(Integer n1, Integer n2) { 
  45.      assertEquals(n1,n2); 
  46.   } 
  47.  
  48.   @Test(dataProvider = "dp_func"
  49.   public void f3(Integer n1, Integer n2) { 
  50.       assertEquals(n1,n2); 
  51.   } 

 運(yùn)行測試用例,可以看到用例成功地進(jìn)行了參數(shù)化,f1執(zhí)行了3次,f2執(zhí)行了3次,f3執(zhí)行了2次,如下圖所示:

測試驅(qū)動(dòng)技術(shù)(TDD)系列之2:詳解TestNG參數(shù)化

 

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2021-02-21 08:53:19

測試驅(qū)動(dòng)技術(shù)數(shù)據(jù)驅(qū)動(dòng)pytest

2021-02-04 07:12:15

測試excelapi

2021-02-04 07:30:14

測試驅(qū)動(dòng)技術(shù)excel讀取數(shù)據(jù)

2009-10-10 10:55:48

TDD技術(shù)

2022-06-17 09:30:00

參數(shù)化測試TestNG測試

2014-04-09 11:13:37

測試驅(qū)動(dòng)開發(fā)

2023-09-11 11:05:49

軟件開發(fā)TDD

2023-12-20 07:12:00

PowerShellCmdletNamed類型

2021-07-02 17:22:50

前端TDDBDD

2010-07-21 16:54:12

EPON

2021-05-18 05:59:45

自動(dòng)化測試TestNgGroup

2021-05-05 11:38:40

TestNGPowerMock單元測試

2023-10-09 07:29:35

算法音樂驅(qū)動(dòng)數(shù)字人

2009-12-15 09:36:32

Visual Stud

2019-04-16 16:23:29

GPU虛擬化CPU

2018-06-15 15:50:34

技術(shù)

2025-02-17 08:00:00

DeepSeek模型AI

2025-02-13 11:00:30

2009-02-12 09:55:28

2020-12-08 12:24:55

接口測試Interface
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)