如何通过testng Dataprovider首先设置参数到第一次测试,第二次设置Paramater到第二次测试ans等等

时间:2016-05-25 05:09:59

标签: testng

这是我的代码,我想将第一组数据{“shankar”,“admin123”}传递给test1() 和第二组数据{“Siva”,“admin @ 123”}到test2(), 请帮帮我,我也试过@工厂......请帮帮我们。

package com.vmetry.dataprovider;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProviderTest {


    @Test(dataProvider="dp")
    public void test1(String uname, String pass){
        System.out.println("the test1 param are: "+uname+"-"+pass);
    }

    @Test(dataProvider="dp")
    public void test2(String uname, String pass){
        System.out.println("the test1 param are: "+uname+"-"+pass);
    }

    //passing data provider data
    @DataProvider(name="dp")
    public Object[][] data(){
        Object[][] obj={{"Vasanth","admin123"},{"Shanmugam","admin@123"}};
        return obj;

    }
}

****************************实际上这是我的情况************** ********** Please CLICK THE IMAGE TO KNOW MY SCENARIO REQUIREMENT

1 个答案:

答案 0 :(得分:0)

在您的示例中,您根本不需要使用DataProvider

public class DataProviderTest {
    @Test
    public void test1(){
        String uname="Shanmugam";
        String pass="admin123";
        System.out.println("the test1 param are: "+uname+"-"+pass);
    }

    @Test
    public void test2(){
        String uname="Shanmugam";
        String pass="admin@123";
        System.out.println("the test1 param are: "+uname+"-"+pass);
    }
}

如果出于某些奇怪的原因,必须使用DataProvider,那么您可以创建其他过滤原始版本的内容:

public class DataProviderTest {
    @Test(dataProvider="dp1")
    public void test1(String uname, String pass){
        System.out.println("the test1 param are: "+uname+"-"+pass);
    }

    @Test(dataProvider="dp2")
    public void test2(String uname, String pass){
        System.out.println("the test1 param are: "+uname+"-"+pass);
    }

    @DataProvider(name="dp1", indices={0})
    public Object[][] data(){
        return data()
    }

    @DataProvider(name="dp2", indices={1})
    public Object[][] data(){
        return data()
    }

    @DataProvider(name="dp")
    public Object[][] data(){
        return new Object[][]{{"Vasanth","admin123"},{"Shanmugam","admin@123"}};
    }
}

或者,如果您希望每个数据提供者记录有一个测试类与您的测试:

public class DataProviderTest {
    private final String uname;
    private final String pass;

    @Factory(dataProvider="dp")
    public DataProviderTest(String uname, String pass) {
        this.uname=uname;
        this.pass=pass;
    }

    @DataProvider(name="dp")
    public static Object[][] data() {
        return new Object[][]{{"Vasanth","admin123"},{"Shanmugam","admin@123"}};
    }

    @Test
    public void test() {
        System.out.println("the test1 param are: "+uname+"-"+pass);
    }
}