How to pass parameter to data provider in testng from csv file

喜你入骨 提交于 2019-12-08 04:11:27

问题


Am reading data from csv file , i have test for which this data will be the input . i want it to run as tescase for every set of value. 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.

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


        }

回答1:


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[][]. 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();
    }


来源:https://stackoverflow.com/questions/26033985/how-to-pass-parameter-to-data-provider-in-testng-from-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!