如何传递参数到testng从csv文件的数据提供者 [英] How to pass parameter to data provider in testng from csv file

查看:185
本文介绍了如何传递参数到testng从csv文件的数据提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从csv文件读取数据,我有测试,这个数据将是输入。
我希望它运行为tescase为每一组值。因为我使用数据提供程序
问题是,它只需要最后一行数据,请帮助我调试代码

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

它只使用最后一行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;


        }


推荐答案

您应该在数据提供程序中读取整个文件,并返回测试用例的迭代器。 HEre是数据提供者的一些伪代码。注意,我使用 List< String []> 来存储测试用例而不是Object [] []。这允许您动态定义测试用例。

You should read whole 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();
    }

这篇关于如何传递参数到testng从csv文件的数据提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆