0

I hope this scenario is bit confused me lot. I want to run a few test cases using junit or testng with different set of data from csv file. The code snippet i have tried is given below but it dint work,

    private static CSVReader csvReader = null;
    @BeforeClass
        public static void setUp() {
            csvReader = new CSVReader(new FileReader(fileName));
        }

    @Test
    public void test1() {
        .......
        .......
        System.out.println(csvReader[0]);
    }

    @Test
    public void test2() {
        .......
        .......
        System.out.println(csvReader[1]);
    }

    @Test
    public void test3() {
        .......
        .......
        System.out.println(csvReader[2]);
    }

    @Test
    public void test4() {
        .......
        .......
        System.out.println(csvReader[3]);
    }

My problem is that i need to use data from each column in different test cases and i need to iterate all the test cases again if i have multiple rows in csv file. I have tried using Theories and Datapoints, but it works in a way that first cases runs with all rows in csv file and its moves to next test case and runs again with all rows in csv.

I want the solution to run test1() with first column of first row, test2() with second column of first row, test3() with third column of first row and test4() with fourth column of first row and then same need to be iterated with second row and so on. Is it possible to iterate the test cases like this ? As far i searched we can iterate a particular test cases in many ways. My question is, is this possible to iterate all the test in a class with one set of data and again reiterate the class with another set of data from csv.

Can we accomplish this using junit or testng? if so, please proved some sample code. Thanks in advance!

Jugi
  • 1,244
  • 3
  • 23
  • 51

2 Answers2

1

Well, there are parameterized tests... You could use them.

@RunWith(Parameterized.class)
public class YourTest {
    @Parameters
    public static Collection<Object[]> data() {
        try( FileReader read = new FileReader(fileName)) {
            CSVReader csvReader = new CSVReader(reader);

            List<CSVRecord> records = ... read data;

            Object[][] parameters = new Object[records.length][1];
            for(int i=0; i<records.length; i++) {
               parameters[i][0] = records.get(i);
            }
            return parameters;
        }
    }

    private CsvRecord record;  // [0] from the array goes here

    public YourTest (CsvRecord record) {
        this.record = record;
    }

    @Test
    public void test() {
         ...do something with the record
    }
}
Florian Schaetz
  • 10,454
  • 5
  • 32
  • 58
  • Thanks @Florian. Exactly what i needed. you have saved my time. But i have made few changes in the above code to make it work. Its great – Jugi Aug 30 '16 at 18:51
  • Of course, the code was just meant as an example, not a complete solution ;-) – Florian Schaetz Aug 30 '16 at 19:00
1

And the TestNG solution is:

public class YourTest {

    @DataProvider
    public static Object[][] data() {
        try( FileReader read = new FileReader(fileName)) {
            CSVReader csvReader = new CSVReader(reader);

            List<CSVRecord> records = ... read data;

            Object[][] parameters = new Object[records.length][1];
            for(int i=0; i<records.length; i++) {
               parameters[i][0] = records.get(i);
            }
            return parameters;
        }
    }

    @Test(dataProvider="data")
    public void test(CsvRecord record) {
         ...do something with the record
    }
}
juherr
  • 5,640
  • 1
  • 21
  • 63
  • This testng approach is not working as expected. if i have 4 test case say a,b,c,d. It should run as a > b > c > d for every row in csv file. Junit works perfectly, – Jugi Sep 19 '16 at 13:30
  • It is supposed to work as you describe. Try with TestNG 6.9.10 and/or 6.9.12 and let me know if it solves the issue. – juherr Sep 19 '16 at 13:32
  • 1
    Could you open an issue on https://github.com/cbeust/testng/issues/ ? It will be easier to discuss about this issue. Thanks! :) – juherr Sep 19 '16 at 14:03