1

I have an excel which contains 4 columns with N rows. Let us say Col1, Col2, Col3, Col4 are the headers. Now I have a java class and there are 3 methods inside it.

    public class test(){ 
     @Test(Dataprovider="test")
     public void mone(String firstval) //firstval will get all the values from Col1 {                       system.out.println(firstval)}
    
     @Test(Dataprovider="test")
     public void mtwo(String secondval, String thirdval, String fourthval) //secondval, thirdval, fourthval will get all the values from Col2, Col3, Col4 { system.out.println(secondval);   system.out.println(thirdval);   system.out.println(fourthval);} 
    
    @DataProvider(name="test")
    public Object[][] getData() {
        String sheetName = "headercomptests";
        int rows = excel.getRowCount(sheetName);
        int cols = excel.getColumnCount(sheetName);     
        Object[][] data = new Object[rows - 1][cols];       
        for (int rowNum = 2; rowNum <= rows; rowNum++) { 
                for (int colNum = 0; colNum < cols; colNum++) {             
                data[rowNum - 2][colNum] = excel.getCellData(sheetName, colNum, rowNum); }
        } return data; } //end of dataprovider class   

How to pass excel column values into different test methods of a class?

Added Edits:

All the columns are in the same worksheet, so if I take two data providers - each one will be starting from the first column. For me, the first method takes values from the 1st column, the second method should take values from Columns 2, 3, 4. So since the excel worksheet is one, data providers will always look from the first.

Added Edits - 10th July. I have tried as you have mentioned, the dataprovider returns the value but the testmethod says throws an error as "Dataprovider mismatch" as it has 5 cols in excel but only one agrument is passed.

  • Your method mone has only one argument and your dataprovider returns 2D object array with 5 values that's the reason exception is thrown. Please be clear what's your required output and post it here – Mohamed Sulaimaan Sheriff Jul 10 at 4:46
0

This would help you

public class TestStatic {

    @DataProvider(name = "test")
    public Object[][] getDataFromDataprovider(Method m) {
        if("TestOne".contentEquals(m.getName())) {
            // Write your data logic for the test method one
            return new Object[][] {{ "TestOne" } };
        }else if("TestTwo".contentEquals(m.getName())) {
            // Write your data logic for the test method two
            return new Object[][] {{ "TestTwo" } };
        }else {
           //You can set default data if method name not exist
            System.out.println("No method found");
            return new Object[][] {{ "Default value" } };
        }
        
    }
    
    @Test(dataProvider="test")
    public void TestOne(String name) {
        System.out.println(name);
    }
    
    @Test(dataProvider="test")
    public void TestTwo(String name) {
        System.out.println(name);
    }

}

Reference: https://testng.org/doc/documentation-main.html#parameters-dataproviders

If you declare your @DataProvider as taking a java.lang.reflect.Method as first parameter, TestNG will pass the current test method for this first parameter. This is particularly useful when several test methods use the same @DataProvider and you want it to return different values depending on which test method it is supplying data for.

| improve this answer | |
  • 1
    Hi Mohamed. I have fixed the code highlighting in your answer. For the future: SO attempts to highlight your code depending on the tags from the question. Since the question was not tagged with java your code snippet looked plain. There is the way to explicitly tell how your code has to be highlighted. Check my edit. – Alexey R. Jul 9 at 10:01
  • Thank you!! I will follow this for my upcoming post – Mohamed Sulaimaan Sheriff Jul 9 at 10:07
0

Alexey, i solved it. i just use a single data provider only. i used an extra class where in i called all the variables from the sheet and then called the method names and passed the column variables. the method body was called outside the called class body. working like a charm . Thanks for the help all.

| improve this answer | |
0

Do not over-complicate your code. Since your tests need different data, use two different data providers. One for each test method. Each data provider would provide its own set of columns.

A bit of details now.. Assume you have this sheet:

enter image description here

And you know how to read the entire data. Say you have the method private Object[][] readSheet(). Now you can create two providers:

Taking the only first column:

@DataProvider(name = "first")
public Object[][] getFirstCol(){
    Object[][] entireSheet = readSheet();
    Object[][] part = new Object[entireSheet.length][1];
    for (int i = 0; i < entireSheet.length; i++){
        part[i][0] = entireSheet[i][0];
    }
    return part;
}

Taking the rest of columns:

@DataProvider(name = "others")
public Object[][] getOtherCols(){
    Object[][] entireSheet = readSheet();
    Object[][] part = new Object[entireSheet.length][TOTAL_COLUMNS - 1];
    for (int i = 0; i < entireSheet.length; i++){
        for(int j = 1; j < entireSheet[i].length; j++) {
            part[i][j - 1] = entireSheet[i][j];
        }
    }
    return part;
}

Having two providers would let you prepare more "clean" and reliable data for your tests. For example the length of different columns could be different, they might have different types and probably might need different pre-processing before feeding your test.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.