简体   繁体   English

如何从csv文件将参数传递给testng中的数据提供者

[英]How to pass parameter to data provider in testng from csv file

Am reading data from csv file , i have test for which this data will be the input .我正在从 csv 文件中读取数据,我进行了测试,该数据将作为输入。 i want it to run as tescase for every set of value.我希望它作为每组值的 tescase 运行。 for that am using data provider The problem is , it is taking only the last set row of data , please help me in debugging the code因为我正在使用数据提供者问题是,它只取最后一组数据,请帮助我调试代码

For eg : if my csv has following data 
name1 id1 text1
name2 id2 text2
name3 id3 text3

it taking only last row name3 id3 text3 and running the test only once not three times.它只需要最后一行 name3 id3 text3 并且只运行一次测试而不是三次。

 @DataProvider(name = "test")
        public Object[][] provider( ) throws InterruptedException
        {

            Object[][] returnObject ;

            String[] checkpoint = ReadfromCSV();

            count = count + 1;

            returnObject = new Object[][]{checkpoint };
            return returnObject;
        }

        @Test(description = "Test", groups =  "test" , dataProvider = "test")
        public void compare(String val1,String val2,String val3,String val4,String val5,String val6,String val7,String val8,String val9,String val10,String val11 ) {

            System.out.println("1:" + val1);

            System.out.println("4:" + val2);

            System.out.println("5:" + val3);


        }
        @SuppressWarnings("null")
        public String[] ReadfromCSV() throws InterruptedException {


            String[] data= null;
            String csvFile = "F:/sample1.csv";
            BufferedReader br = null;
            String line = "";
            String cvsSplitBy = ",";

            try {

                br = new BufferedReader(new FileReader(csvFile));
                while ((line = br.readLine()) != null) {

                    // use comma as separator
                data= line.split(cvsSplitBy);




                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            System.out.println("Done");
            return data;


        }

You should read entire file in data provider and return iterator of test cases.您应该在数据提供程序中读取整个文件并返回测试用例的迭代器。 Here is some pseudocode for data provider.这是数据提供者的一些伪代码。 Notice that I used List<String []> to store test cases instead of Object[][] .请注意,我使用List<String []>而不是Object[][]来存储测试用例。 This allows you do define test cases dynamically.这允许您动态定义测试用例。

    @DataProvider(name = "test")
    public Iterator<Object []> provider( ) throws InterruptedException
    {
        List<Object []> testCases = new ArrayList<>();
        String[] data= null;

        //this loop is pseudo code
        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {
            // use comma as separator
            data= line.split(cvsSplitBy);
            testCases.add(data);
        }

        return testCases.iterator();
    }
public String[][] ReadfromCSV() throws InterruptedException {

    int count =0;
    String[] data= null;
    String returnObj[][] = null; 
    
    //System.out.println(System.getProperty("user.dir"));
    String csvFile = System.getProperty("user.dir")+ "/src/test/resources/testdata.csv";
            
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";
    ArrayList<String> content = new ArrayList<String>();

    try {

        //this loop is pseudo code
        br = new BufferedReader(new FileReader(csvFile));
        int datalength = 0;
        int listsize =0;;
        
        while ((line = br.readLine()) != null) {
            // use comma as separator

            content.add(line);
        }
        System.out.println(content);
        
        listsize = content.size();
        datalength = content.get(0).split(cvsSplitBy).length;
        returnObj = new String[listsize][datalength];
        
        for (int i = 0; i<listsize; i++) {
            
            data = content.get(i).split(cvsSplitBy);
            for (int j=0; j< datalength ; j++) {
                returnObj[i][j] = data[j];
                
            }
            
        }
        

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    System.out.println("Done");
    return returnObj;

}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何将参数和数据提供者传递给Testng中的相同方法 - How can we pass Parameter and Data Provider to same Method in Testng TestNg 当数据输入提供者是 CSV 文件时打印参数的值 - TestNg Printing the value of the parameters when the data input provider is a CSV file Selenium Webdriver,TestNG-数据提供程序尝试传递2个参数,但是方法采用3个参数,而TestNG无法注入合适的对象 - Selenium Webdriver, TestNG - data provider is trying to pass 2 parameter but the method take 3 and TestNG is unable in inject a suitable object 如何从 excel 表中获取单行并使用 java 传递到 testng 数据提供程序 - How to fetch single row from excel sheet and pass into testng data provider using java Json 文件作为 TestNG 中的数据提供者 - Json file as data provider in TestNG 如何将参数从1类文件中的@Test方法传递到另一个类文件中的注释方法(testNG) - how to pass parameter from @Test method in 1 class file to a annotated method in another class file(testNG) 如何从TestNG数据提供者跳过数据集(出错)? - How to skip a data set (on error) from TestNG data provider? 如何将参数从 testNG.xml 文件传递到 cucumber 场景/步骤 - How to pass a parameter from testNG.xml file to cucumber Scenarios/steps 如何将TestNG参数传递到黄瓜中? - How to pass TestNG parameter into Cucumber? 如何使用Mockito TestNG在数据提供程序中传递模拟对象? - How can I pass mock object in data provider, using Mockito TestNG?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM