您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > TestNG
TestNG參數化測試
作者:網絡轉載 發(fā)布時間:[ 2016/1/20 13:28:23 ] 推薦標簽:單元測試工具 軟件測試

  傳遞參數與數據提供者
  當你需要通過復雜的參數或參數需要創(chuàng)建從Java(復雜的對象,對象讀取屬性文件或數據庫等..),在這種情況下,可以將參數傳遞使用數據提供者。數據提供者@DataProvider的批注的方法。這個注解只有一個字符串屬性:它的名字。如果不提供名稱,數據提供者的名稱會自動默認方法的名稱。數據提供者返回一個對象數組。
  讓我們看看下面的例子使用數據提供者。第一個例子是@DataProvider的使用Vector,String或Integer 作為參數,第二個例子是關于@DataProvider 的使用對象作為參數。
  實例 1
  在這里 @DataProvider 通過整數和布爾參數。
  創(chuàng)建Java類
  創(chuàng)建一個java類PrimeNumberChecker.java。這個類檢查,如果是素數。創(chuàng)建這個類在 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)建測試案例類
  創(chuàng)建一個Java測試類 ParamTestWithDataProvider1.java.
  定義方法primeNumbers(),其定義為DataProvider 使用注釋。此方法返回的對象數組的數組。
  測試方法testPrimeNumberChecker()添加到測試類中。此方法需要一個整數和布爾值作為輸入參數。這個方法驗證,如果傳遞的參數是一個素數。
  添加注釋 @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í)行測試案例。
  <?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:TestNG_WORKSPACE>.javac ParamTestWithDataProvider1.java PrimeNumberChecker.java
  運行testng.xml.
  C:TestNG_WORKSPACE>java -cp "C:TestNG_WORKSPACE" org.testng.TestNG testng.xml
  驗證輸出。
  2 true
  6 false
  19 true
  22 false
  23 true
  ===============================================
  Suite1
  Total tests run: 5, Failures: 0, Skips: 0
  ===============================================
  實例 2
  在這里,@DataProvider 傳遞對象作為參數。
  創(chuàng)建Java類
  創(chuàng)建一個Java類 Bean.java, 對象帶有 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)建測試案例類
  創(chuàng)建一個Java測試類 ParamTestWithDataProvider2.java.
  定義方法primeNumbers(),其定義為DataProvider使用注釋。此方法返回的對象數組的數組。
  添加測試類中測試方法TestMethod()。此方法需要對象的bean作為參數。
  添加注釋 @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)建一個文件 testng.xml C: > TestNG_WORKSPACE 來執(zhí)行測試用例.
  <?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:TestNG_WORKSPACE>javac ParamTestWithDataProvider2.java Bean.java
  運行 testng.xml.
  C:TestNG_WORKSPACE>java -cp "C:TestNG_WORKSPACE" org.testng.TestNG testng.xml
  驗證輸出。
  hi I am the bean 111
  ===============================================
  Suite1
  Total tests run: 1, Failures: 0, Skips: 0
  ===============================================

上一頁12下一頁
軟件測試工具 | 聯系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd