Article Writing TestNG - provider of data remembered a mini framework'u to perform unit tests, which came to me one day to write. I had to write it, not because the project was not known jUnit) but because jUnit in some cases not much help. In practical terms, the situation when many tests have the same algorithm, and differ in test data (sometimes they are quite large data sets).
my opinion in this case, we separately manage the data and test algorithms, and place it in different files. Then there is the possibility that the test data prepared by someone other than the test algorithms, no need to recompile the test when the data has changed, and no easier to avoid problems with the encoding of input data.
DataProvider in TestNG does not directly zaczytywania test data from XML files. But as it turned out, doing it yourself is not difficult, and most importantly does not require significant coding effort. Simply use the specifications provided by the J2SE XML serialization JavaBean'ów.
Schema serialization is relatively understandable to people, and most importantly is very flexible. This schema allows to save any number of custom objects. In addition, you can easily generate a sample input file for testing by zserializowanie crafted in the code sample data.
Below is an example of the contents of the XML file with test data:
\u0026lt;? Xml version = "1.0" encoding="UTF-8" ?>However, implementation TestNG'owego data provider that uses this XML file might look like this: package com
<java version="1.5.0" class="java.beans.XMLDecoder">
<array>
<string>test1</string>
<object class="pl.dwalczak.testngdp1.User">
<void property="nickName">
<string>heniek</string>
</void>
<void property="address">
<object class="pl.dwalczak.testngdp1.Address"
id="addr1">
<void property="city">
<string>Poznań</string>
</void>
<void property="postcode">
<string>11-111</string>
</void>
<void property="street">
<string>Jadwigi</string>
</void>
<void property="number">
<string>11a/3</string>
</void>
</object>
</void>
<void property="mailingAddress">
<object idref="addr1" />
</void>
</object>
</array>
<array>
<string>test2</string>
<object class="pl.dwalczak.testngdp1.User">
<void property="nickName">
<string>maniek</string>
</void>
<void property="address">
<object idref="addr1" />
</void>
<void property="mailingAddress">
<object IDREF = "Addr1" /> \u0026lt;/ void> \u0026lt;/ object> \u0026lt;/ array> \u0026lt;/ java>
. dwalczak.testngdp1; java.beans.XMLDecoder import, import java.io.FileInputStream; import java.util.ArrayList; org.testng.annotations.DataProvider import, import org.testng.annotations.Test; @ Test public class SimpleTest { @ DataProvider (name = "dp") public Object [] [] createData () {ArrayList result = new \u0026lt;Object[]> ArrayList<Object[]>();
XMLDecoder dec = null;
try {
dec = new XMLDecoder(new FileInputStream("testData.xml"));
while (true) {
Object o = dec.readObject();
result.add((Object[]) o);
}
} catch (ArrayIndexOutOfBoundsException e) {
// no more objects in stream
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (dec != null) {
dec.close();
}
}
return result.toArray(new Object[result.size()][]);
}
@Test(dataProvider="dp")
public void test(String arg0, User user) {
System.out.println("arg0: " + arg0) System.out.println ("user" + user);}}
Resources
example source filesLong Term Persistence of JavaBeans Components: XML Schema
TestNG documentation - 5.5.2 - From DataProviders
0 comments:
Post a Comment