Reputation: 63734
I'm familiar with the use of Parameterized tests in JUnit, e.g:
http://junit.org/apidocs/org/junit/runners/Parameterized.html
but I wondered whether there were any alternative (possibly better) approaches to externally defined test data. I don't really want to hardcode my test data in my source file - I'd prefer to define it in XML or in some sort of other structured manner.
Is there a particular framework, or extension to a framework that provides me with a better solution than the standard JUnit approach. What specifically was the best approach you found to this?
EDIT: I'm familiar with the FIT framework and the use of it in this case, I want to avoid that if possible. If there is a JUnit extension or similar framework that provides a better solution than TestNG then please let me know.
Upvotes: 1
Views: 604
Reputation: 26281
There is nothing I see in the parameterized test example that requires you to store the data in the class itself. Use a method that pulls the data from an xml file and returns Object[][] and call that where in the example they use static code. If you want to switch the TestNG, of course, they have already written an XML parser for you. It looks like JUnitExt has one as well for JUnit 4, or you could write one of your own.
Upvotes: 0
Reputation: 3640
You can try Dependency Injection or Inversion of Control for this. The Spring Framework does this.
Upvotes: 1
Reputation: 63734
So I found TestNG's approach to this, which allows you to specify parameters for tests, more info available here:
http://testng.org/doc/documentation-main.html#parameters-testng-xml
An example of this would be:
@Parameters({ "first-name" })
@Test
public void testSingleString(String firstName) {
System.out.println("Invoked testString " + firstName);
assert "Cedric".equals(firstName);
}
and:
<suite name="My suite">
<parameter name="first-name" value="Cedric"/>
<test name="Simple example">
You can also use a datasource (such as Apache Derby) to specify this testdata, I wonder how flexible a solution this is though.
Upvotes: 1