1

I have a single jUnit4 test case with many variables. I read these variables from an external csv file. What I would like to have is that the test would run once for each line of input parameters from the file. I reckon this can be done by using @Parametrized.

I am not sure how though...

Right now I have something like this...

@Test
...
readFromCSV(filename);
String carName = varFromFile[0];
selectItemFromList(fieldName, carName);

The carName is a record in the csv file. How should I change this to have several entries for the carName taken from the file and one test ran for each one?

EDIT: Two more things. the read from file looks like this

    CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), ';');
    String [] nextValue;
    String [] header = reader.readNext();
    System.out.println(header[0] + " : " + header[1] + "etc...");
    while ((nextValue = reader.readNext()) != null) {
        System.out.println(nextValue[0] + " : " + nextValue[1] + "etc...");
    }

The parametrized test...

@RunWith(value = Parameterized.class)
public class testParametrized {

     private final TestParameters parameters;

     public static class TestParameters {
         String recId;
         String carName;
     }

     public testParametrized(TestParameters parameters) {
        this.parameters = parameters;
        System.out.println(parameters);
     }

     @Parameters
     public static Collection<Object[]> data() throws Exception {
       Object[][] data = new Object[][] { 
               { new TestParameters() }, 
               { new TestParameters() }
               };
       return Arrays.asList(data);
     }

     @Test
     public void pushTest() {
       System.out.println("Parameters : " + parameters);
     }

}

How do I connect the two to have a test that takes parameters from csv file ?

4

2 に答える 2

2

Check this out:

@RunWith(org.junit.runners.Parameterized.class)
public class Test {

    private final TestParameters parameters;

    public static class TestParameters {
        int parameter1;
        String parameter2;
    }

    @Parameters
    public static Collection<Object[]> data() {
            // you can read you csv file here 
        return Arrays.asList(new Object[] { new TestParameters() }, new Object[] { new TestParameters() });
    }

    public Test(TestParameters parameters) {
        this.parameters = parameters;
        System.out.println(parameters);
    }

    @org.junit.Test
    public void test1() {
        System.out.println("test1: " + parameters);
    }
}

}

this data method (annotated with @Parameters) must return collection of constructor arguments array and for each entry in this collection new instance of test class will be created and test will be executed

于 2012-07-25T09:33:55.863 に答える
2

This works fine

private TestParameters parameters;

public static class TestParameters {
//variables
}

public myTest(TestParameters parameters) {
    this.parameters = parameters;
}

@Parameters
public static Collection<Object[]> data() throws Exception {
    Collection<Object[]> parametersProvided = new ArrayList<Object[]>();
    String[][] datas = readFromCSV("data.csv");
    for (String[] nextValue : datas) {
        TestParameters testContainer = new TestParameters();
        //variables
        //
        parametersProvided.add(new Object[] { testContainer });
    }
    return parametersProvided;
}

@Test
public void pushTest() {
    System.out.println("Parameters : " + parameters);
}
于 2012-07-26T11:49:19.180 に答える