views:

199

answers:

3

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.

+1  A: 

You can try Dependency Injection or Inversion of Control for this. The Spring Framework does this.

Joset
"Best" is a matter of opinion. Google Guice also works quite well.
Dave W. Smith
How does this help me with parameterized testdata though?
Jon
I'm not sure Spring helps here, sure it alleviates unit/integration testing, but not in terms of parameterized data for tests. It seems a bit overkill to drop Spring into my application to achieve this also. Many thanks anyways.
Jon
@ Dave W. Smith: point taken.
Joset
+1  A: 

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.

Jon
A: 

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.

Kathy Van Stone