1. 创建 Java 测试类
2. 添加测试方法 TestngParameterTest(String name, String age)
3. 为测试方法添加注释 @Parameters({"name", "age"})
创建 Java 类:
1 package roger.testng; 2 3 import org.testng.annotations.Parameters; 4 import org.testng.annotations.Test; 5 6 public class TestngParameterTest { 7 @Test 8 @Parameters({"name", "age"}) 9 public void parameterTest(String name, String age) { 10 System.out.println("name: " + name + ", age: " + age); 11 } 12 }
创建 testng.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <suite name="testngTest"> 3 <test name="test1"> 4 <parameter name="name" value="roger" /> 5 <parameter name="age" value="27" /> 6 <classes> 7 <class name="roger.testng.TestngParameterTest" /> 8 </classes> 9 </test> 10 </suite>
运行 testng.xml 文件
[TestNG] Running:
E:Selenium_NewJavaForSeleniumLearning estng.xml
name: roger, age: 27
===============================================
testngTest
Total tests run: 1, Failures: 0, Skips: 0
===============================================