2--TestNG 参数化

(1)XML文件

public class ParameterTest{

  @test

  @Parameters({"name","age"})

  public void paramTest1(Strng name, int age){

    system.out.println("name="+name+"; age="+age);

}

}

(2) Parameter.xml

<?xml version="1.0" encoding="UTF-8"?>

  <suite name ="parameter">

    <test name="param">

      <classes>

        <parameter name="name" value="zhangsan"/>

        <parameter name="age" value="10"/>

        <class name="com.course.testng.parameter.ParameterTest"/>

      </classes>

    </test>

  </suite>

结果:name=zhangsan; age=10

(2)Dataprovider

public class DataproviderTest{

  @test(dataProvider="data")

  public void testDaraProvider(Strng name, int age){

    system.out.println("name="+name+"; age="+age);

}

@Dataprovider(name="data")

public Object[][] providerData(){

Object[][] o= new Object[][]{

{"zhangsan",10},

{"lisi",20}

}

}

}

结果:

name=zhangsan; age=10

name=lisi; age=20

如何按照方法使用不同的参数?

public class DataproviderTest{

  @test(dataProvider="methodData")

  public void test1(Strng name, int age){

    system.out.println("name="+name+"; age="+age);

}

  @test(dataProvider="methodData")

  public void test2(Strng name, int age){

    system.out.println("name="+name+"; age="+age);

}

import java.lang.reflect.Method;//反射包

@Dataprovider(name="methodData")

public Object[][] methodDataTest(Method method){

Object[][] result= null;

if (method.getName.equals("test1")){

result=new Object[][]{

{"zhangsan",20},

{"lisi",25}

}

}

else if (method.getName().equals("test2")){

result=new Object[][]{

{"wangwu",50},

{"zhaoliu",60}

}

return result;

}

}

上一篇:来自后端的突袭? --开包即食的教程带你浅尝最新开源的C# Web引擎 Blazor


下一篇:JS字符串格式化函数 string.format