Using 1 DataProvider to handle multiple CSV files?

656 views
Skip to first unread message

Ed Abshire

unread,
Aug 27, 2010, 7:51:43 PM8/27/10
to testng-users
Howdy, I've been looking around on this and can't seem to find
anything.

I understand the concept of the @Data and @DataProvider annotations,
however I was wondering if a means exsists to simply say
@Data(name="some.csv") so that testers can specify data files for
data driving methods without having to create a @DataProvider for each
test method?

(Or if there was a way to have a single @DataProvider method support a
passed in csv file name?)

-Ed A.

Cédric Beust ♔

unread,
Aug 27, 2010, 9:56:51 PM8/27/10
to testng...@googlegroups.com
Hi Ed,

First of all, TestNG only has @DataProvider, I'm not sure where you read about @Data...

There are several ways to do this, here is one:

public class BaseCsv {
  private String fileName;
  public BaseCsv(String n) { this.fileName = n; }

  @DataProvider
  public Object[][] csv() {
    // parse the file name, return the object
  }
}

public class MyTest extends BaseCsv {

  @Factory
  public Object[] {
    return new Object[] {
      new MyTest("a.csv"),
      new OtherTest("b.csv"),
  }

  @Test(dataProvider = "csv")
  ...
}

You could also use parameters specified in testng.xml.

I'm sure plenty of people are using CSV files, can you share your approach?

--
Cédric



--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.




--
Cédric


Ansgar Konermann

unread,
Aug 28, 2010, 7:55:29 PM8/28/10
to testng...@googlegroups.com

Hi Ed,

have a look at http://github.com/ansgarkonermann/testng-goodies, where I
tried something similar for Enums and included a learning test. You
could use this approach to create a generic CSV-based data provider.

The trick is: any data provider method might take a java.lang.Method
parameter, referencing the test method the data provider is attached to,
so you can evaluate more specific annotations of this method from within
the data provider.

Hope this helps a bit.

Kind regards

Ansgar

Reply all
Reply to author
Forward
0 new messages