A Data Provider serves two purposes:
This data provider is a basic Provider that reads data set from a simple XML file, let's explain how to use it through an basic example.
Given the follow fake test case:
package org.mycompany.myapp;
class MyTestCase {
public void verifyTypeCorrectnessTypeConversion(final Date date, final String message, final int integer) {
...
}
}
that needs the XML Data Provider, first you need to annotate the test method:
@org.testng.annotations.Test(
dataProvider = com.testnguice.providers.XMLDataProvider.DATA_PROVIDER_ID,
dataProviderClass = com.testnguice.providers.XMLDataProvider.class
)
public void verifyTypeCorrectnessTypeConversion(final Date date, final String message, final int integer) {
...
}
The Data Provider will read data to send to the method from an XML classpath resource, which location pattern is very easy:
${qualifiedClassName}.${testMethodName}.test-data.xml
So, contextualized to the example, for the method
org.mycompany.myapp.MyTestCase#verifyTypeCorrectnessTypeConversion
The relative classpath resource is
org/mycompany/myapp/MyTestCase.verifyTypeCorrectnessTypeConversion.test-data.xml
Let's speak about the simple XML format of the file: the structure is an xml-dataprovider that contains a list of test elements that contains a list of argument; each argument element reflects the natural order of the test method arguments, additional arguments will be ignored.
Users can use the schema uploaded on testnguice server to build their own data sources, please reference that location with the follow namespace:
xmlns:xdp="http://testnguice.com/schemas/2.0/xml-dataprovider"
Follows a brief example:
<xdp:xml-dataprovider xmlns:xdp="http://testnguice.com/schemas/2.0/xml-dataprovider"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://testnguice.com/schemas/2.0/xml-dataprovider
http://testnguice.googlecode.com/svn/schemas/2.0/xml-dataprovider.xsd">
...
<xdp:test>
<xdp:argument>2009-11-03T12:00:30</xdp:argument>
<xdp:argument>another string</xdp:argument>
<xdp:argument>17</xdp:argument>
</xdp:test>
...
</xdp:xml-dataprovider>
Warning The resource classpath will be parsed with a non-validating parser to speed up the test, so please make sure you wrote/generated the data source in the right syntax.
The 'String to Java' objects binding in charge of the apache commons-beanutils converter package, so feel free to register you favorite converter:
import java.util.Date;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
...
DateConverter dateConverter = new DateConverter();
dateConverter.setPatterns(new String[] {
"yyyy",
"yyyy-MM",
"yyyy-MM-dd",
"yyyy-MM-dd'T'HH",
"yyyy-MM-dd'T'HH:mm",
"yyyy-MM-dd'T'HH:mm:ss"
});
ConvertUtils.register(dateConverter, Date.class);
and then execute your tests, the XML provider will resolve it on run-time and bind the Object.