As mentioned in the article TestNG - first steps . TestNG Framework lets you define the parameters in the configuration file, which then can be used as arguments to methods of testing.
But TestNG is also available in a much more powerful mechanism for parameterization testing data. It is called. supplier data - data provider.
Data Provider is defined as a method denoted
DataProvider annotation
. This method should return a two-dimensional array of objects ( Object [] []
) or the iterator after the one-dimensional arrays of objects (Iterator \u0026lt;Object[]>
). And the following array of objects returned by the iterator (or those in two-dimensional array) containing the arguments to the next call (as many times as these tables) of the test method. method of data provider may be bezargumentowa or have an argument of type java.lang.reflect.Method
, which defines tester method, for which the data will be provided. To assign a data provider to the test method, you must properly set the parameter
dataProvider for the annotation
test, which is marked with the Testing Method. Optionally, if the method is a test on a different class than the method to provide data, set the parameter yet dataProviderClass
annotations
Test. In addition, the test method argument types must be compatible with the types of objects in the arrays returned by the data provider. Unfortunately you can not use the provider, including data and parameters defined in the configuration file. You also can not pass these parameters to the methods of data providers. Below
stir example of using the data provider TestNG:
pl.dwalczak.jpapg2 package, import org.apache.log4j.Logger; org.testng.Assert import, import org.testng.annotations.DataProvider imports, org.testng.annotations.Test imports, pl.dwalczak.jpapg2.model.Address imports, pl.dwalczak.jpapg2.model.User; @ public class Test extends UserTest JpaTestCase {private static final Logger LOG = Logger.getLogger (UserTest.class) @ DataProvider (name = "address") public Object [] [] createTestData () {LOG.debug ("createTestData"); return new Object [] [] {{"Heniek", new Address ("Poznan", "11-111", "Hedwig", "12c/23")}, {"Manny", new Address ("Wroclaw, "22-222", "Kosciuszko", "14b ")}};} @ Test (dataProvider =" address ") public void createuser (String user, Address address) {LOG.debug (" create user "+ user) ; em.persist (new User (user, address, address));} @ Test (dependsOnMethods = {"createuser"}, dataProvider = "address") public void findUser (String user, Address address) {LOG.debug (" find user: "+ user); User u = (User) em.createNamedQuery (user.findByNickName"). setParameter ("nickname" user). getSingleResult (); LOG.debug ("Found user:" + u); Assert.assertNotNull (u); Assert.assertEquals (address, u.getAddress ());}}
Resources
Example Source FilesTestNG documentation - 5.5.2 - From DataProviders
0 comments:
Post a Comment