如何在 TestNG 中執行資料引數化?


在 TestNG 中,我們可以執行資料引數化。可以在 TestNG 執行中透過以下方法執行引數化 -

  • 使用 @Parameters 註解進行資料引數化。

  • 使用 @DataProvider 註解進行資料引數化。

示例

使用 @Parameter 註解實現 TestNG xml 檔案的程式碼。

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Tutorialspoint Test">
   <parameter name = "Url" value="https://www.tutorial.com"/>
   <test name = "Regression Cycle 1">
      <classes>
         <class name = "TestParameter" />
      </classes>
   </test>
</suite>

我們可以透過在 TestNG xml 檔案中定義 <parameter> 來在執行時將值傳遞給測試方法。

示例

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestParameter {
   @Test
   @Parameters("Url")
   public void loginwithUrl(String urlname) {
      System.out.println("The value of url is : " + urlname);}
   }

Java 類檔案中帶有 @Parameters (“Url”)。

使用 @DataProvider 註解實現的程式碼。

@DataProvider(name = "QuestionSearch")
public Object[][] quest_anssearch(){
   return new Object[][]
   {
      { “Tutorialspoint”, “Java”},
      { “Python”, “PyCharm”},
   };
}
@Test(dataProvider = "QuestionSearch ")
public void userInput(String subject, String lang){
   System.out.println("The values are : " + subject +”“+ lang);
}

我們可以藉助 Java 類檔案中的 @DataProvider 在執行時傳遞多個數據。

更新時間:2021-11-19

268 views

開啟你的 職業生涯

完成該課程即可獲得認證

開始
廣告
© . All rights reserved.