• 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;

    }

    }

  • 相关阅读:
    解决死锁四大方式
    Windows内存管理简介:
    排序算法优劣
    排序
    HTTPs
    http和https的异同
    HTTP协议
    FTP与TFTP
    tomcat热部署
    开发心得体会
  • 原文地址:https://www.cnblogs.com/lizhiyan/p/10552735.html
Copyright © 2020-2023  润新知