您的位置:軟件測(cè)試 > 開源軟件測(cè)試 > 開源單元測(cè)試工具 > TestNG
TestNG參數(shù)化測(cè)試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2016/1/20 13:28:23 ] 推薦標(biāo)簽:單元測(cè)試工具 軟件測(cè)試

  傳遞參數(shù)與數(shù)據(jù)提供者
  當(dāng)你需要通過(guò)復(fù)雜的參數(shù)或參數(shù)需要?jiǎng)?chuàng)建從Java(復(fù)雜的對(duì)象,對(duì)象讀取屬性文件或數(shù)據(jù)庫(kù)等..),在這種情況下,可以將參數(shù)傳遞使用數(shù)據(jù)提供者。數(shù)據(jù)提供者@DataProvider的批注的方法。這個(gè)注解只有一個(gè)字符串屬性:它的名字。如果不提供名稱,數(shù)據(jù)提供者的名稱會(huì)自動(dòng)默認(rèn)方法的名稱。數(shù)據(jù)提供者返回一個(gè)對(duì)象數(shù)組。
  讓我們看看下面的例子使用數(shù)據(jù)提供者。第一個(gè)例子是@DataProvider的使用Vector,String或Integer 作為參數(shù),第二個(gè)例子是關(guān)于@DataProvider 的使用對(duì)象作為參數(shù)。
  實(shí)例 1
  在這里 @DataProvider 通過(guò)整數(shù)和布爾參數(shù)。
  創(chuàng)建Java類
  創(chuàng)建一個(gè)java類PrimeNumberChecker.java。這個(gè)類檢查,如果是素?cái)?shù)。創(chuàng)建這個(gè)類在 C: > TestNG_WORKSPACE
  public class PrimeNumberChecker {
  public Boolean validate(final Integer primeNumber) {
  for (int i = 2; i < (primeNumber / 2); i++) {
  if (primeNumber % i == 0) {
  return false;
  }
  }
  return true;
  }
  }
  創(chuàng)建測(cè)試案例類
  創(chuàng)建一個(gè)Java測(cè)試類 ParamTestWithDataProvider1.java.
  定義方法primeNumbers(),其定義為DataProvider 使用注釋。此方法返回的對(duì)象數(shù)組的數(shù)組。
  測(cè)試方法testPrimeNumberChecker()添加到測(cè)試類中。此方法需要一個(gè)整數(shù)和布爾值作為輸入?yún)?shù)。這個(gè)方法驗(yàn)證,如果傳遞的參數(shù)是一個(gè)素?cái)?shù)。
  添加注釋 @Test(dataProvider = "test1") 到此方法。dataProvider的屬性被映射到"test1".
  創(chuàng)建Java類文件名ParamTestWithDataProvider1.java 在 C: > TestNG_WORKSPACE
  import org.testng.Assert;
  import org.testng.annotations.BeforeMethod;
  import org.testng.annotations.DataProvider;
  import org.testng.annotations.Test;
  public class ParamTestWithDataProvider1 {
  private PrimeNumberChecker primeNumberChecker;
  @BeforeMethod
  public void initialize() {
  primeNumberChecker = new PrimeNumberChecker();
  }
  @DataProvider(name = "test1")
  public static Object[][] primeNumbers() {
  return new Object[][] { { 2, true }, { 6, false }, { 19, true },
  { 22, false }, { 23, true } };
  }
  // This test will run 4 times since we have 5 parameters defined
  @Test(dataProvider = "test1")
  public void testPrimeNumberChecker(Integer inputNumber,
  Boolean expectedResult) {
  System.out.println(inputNumber + " " + expectedResult);
  Assert.assertEquals(expectedResult,
  primeNumberChecker.validate(inputNumber));
  }
  }
  創(chuàng)建 TESTNG.XML
  創(chuàng)建 testng.xml C: > TestNG_WORKSPACE 執(zhí)行測(cè)試案例。
  <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
  <suite name="Suite1">
  <test name="test1">
  <classes>
  <class name="ParamTestWithDataProvider1" />
  </classes>
  </test>
  </suite>
  編譯使用javac的測(cè)試用例類。
  C:TestNG_WORKSPACE>.javac ParamTestWithDataProvider1.java PrimeNumberChecker.java
  運(yùn)行testng.xml.
  C:TestNG_WORKSPACE>java -cp "C:TestNG_WORKSPACE" org.testng.TestNG testng.xml
  驗(yàn)證輸出。
  2 true
  6 false
  19 true
  22 false
  23 true
  ===============================================
  Suite1
  Total tests run: 5, Failures: 0, Skips: 0
  ===============================================
  實(shí)例 2
  在這里,@DataProvider 傳遞對(duì)象作為參數(shù)。
  創(chuàng)建Java類
  創(chuàng)建一個(gè)Java類 Bean.java, 對(duì)象帶有 get/set 方法, 在 C: > TestNG_WORKSPACE.
  public class Bean {
  private String val;
  private int i;
  public Bean(String val, int i){
  this.val=val;
  this.i=i;
  }
  public String getVal() {
  return val;
  }
  public void setVal(String val) {
  this.val = val;
  }
  public int getI() {
  return i;
  }
  public void setI(int i) {
  this.i = i;
  }
  }
  創(chuàng)建測(cè)試案例類
  創(chuàng)建一個(gè)Java測(cè)試類 ParamTestWithDataProvider2.java.
  定義方法primeNumbers(),其定義為DataProvider使用注釋。此方法返回的對(duì)象數(shù)組的數(shù)組。
  添加測(cè)試類中測(cè)試方法TestMethod()。此方法需要對(duì)象的bean作為參數(shù)。
  添加注釋 @Test(dataProvider = "test1") 到此方法. dataProvider 屬性被映射到 "test1".
  創(chuàng)建Java類文件名 ParamTestWithDataProvider2.java 在 C: > TestNG_WORKSPACE
  import org.testng.annotations.DataProvider;
  import org.testng.annotations.Test;
  public class ParamTestWithDataProvider2 {
  @DataProvider(name = "test1")
  public static Object[][] primeNumbers() {
  return new Object[][] { { new Bean("hi I am the bean", 111) } };
  }
  @Test(dataProvider = "test1")
  public void testMethod(Bean myBean) {
  System.out.println(myBean.getVal() + " " + myBean.getI());
  }
  }
  創(chuàng)建 TESTNG.XML
  創(chuàng)建一個(gè)文件 testng.xml C: > TestNG_WORKSPACE 來(lái)執(zhí)行測(cè)試用例.
  <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
  <suite name="Suite1">
  <test name="test1">
  <classes>
  <class name="ParamTestWithDataProvider2" />
  </classes>
  </test>
  </suite>
  編譯使用javac的測(cè)試用例類。
  C:TestNG_WORKSPACE>javac ParamTestWithDataProvider2.java Bean.java
  運(yùn)行 testng.xml.
  C:TestNG_WORKSPACE>java -cp "C:TestNG_WORKSPACE" org.testng.TestNG testng.xml
  驗(yàn)證輸出。
  hi I am the bean 111
  ===============================================
  Suite1
  Total tests run: 1, Failures: 0, Skips: 0
  ===============================================

上一頁(yè)12下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd