如何在TestNG中执行参数化?
可以通过以下方式在TestNG中执行参数化-
带有@Parameters批注的数据参数化。
@DataProvider批注进行数据参数化。
示例
使用@Parameter注释的Testngxml文件。
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Nhooo Test">
<parameter name = "Url" value="https://www.tutorial.com"/>
<test name = "Regression Cycle 1">
<classes>
<class name = "TestParameter" />
</classes>
</test>
</suite>通过在testngxml文件中定义<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[][]{
{ “Nhooo” , “Java”},
{ “Python” , “PyCharm”},
};
}
@Test(dataProvider = "QuestionSearch ")
public void userInput(String subject, String lang){
System.out.println("The values are : " + subject +”“+ lang);
}我们可以在运行时在Java类文件中通过@DataProvider传递多个数据。