Question

I am trying to achieve lazy loading of data provider for my tests but it is not working . Below is my code snippet for factory annotated method:

import java.util.Iterator;

import org.testng.annotations.AfterClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

public class TF
{
    @DataProvider(name="TestInstances")
    public static Iterator<Object> testInstancesDP()
    {
        return new Iterator<Object>()
        {
            int counter=1;
            int MAX;
            {
                MAX=4;
            }

            @Override
            public boolean hasNext()
            {
                return counter<=MAX;
            }

            @Override
            public Object next()
            {
                System.out.println("passing "+counter+" iterator instance");
                return new Object[] {counter++};
            }

            @Override
            public void remove()
            {
                //TODO: IllegalOperationException
            }
        };
    }

    private int order;

    @Factory(dataProvider="TestInstances",dataProviderClass=TF.class)
    public TF(int order)
    {
        this.order=order;
        System.out.println("TF Instance created : "+order);
    }

    @DataProvider
    public Iterator<Object> testDataProvider()
    {
        return new Iterator<Object>()
        {
            int counter=1;
            int MAX;
            {
                MAX=TF.this.order;
            }

            @Override
            public boolean hasNext()
            {
                return counter<=MAX;
            }

            @Override
            public Object next()
            {
                System.out.println("passing "+counter+" as DP");
                return new Object[] {"DP"+counter++};
            }

            @Override
            public void remove()
            {
                //TODO: IllegalOperationException
            }

        };
    }

    @Test(dataProvider="testDataProvider")
    public void testStepExecutor(String str)
    {
        System.out.println("Test method execution : "+str);
    }

    @AfterClass
    public void tearDown()
    {
        System.out.println("Tear Down: "+this.order);
    }

    /**
     * @return the order
     */
    public int getOrder()
    {
        return order;
    }
}
  • Actual Output:

    passing 1 iterator instance TF Instance created : 1 passing 2 iterator instance TF Instance created : 2 passing 3 iterator instance TF Instance created : 3 passing 4 iterator instance TF Instance created : 4 [TestNG] Running: Command line suite

    passing 1 as DP Test method execution : DP1 Tear Down: 1 passing 1 as DP Test method execution : DP1 passing 2 as DP Test method execution : DP2 Tear Down: 2 passing 1 as DP Test method execution : DP1 passing 2 as DP Test method execution : DP2 passing 3 as DP Test method execution : DP3 Tear Down: 3 passing 1 as DP Test method execution : DP1 passing 2 as DP Test method execution : DP2 passing 3 as DP Test method execution : DP3 passing 4 as DP Test method execution : DP4 Tear Down: 4

    =============================================== Command line suite

    Total tests run: 10, Failures: 0, Skips: 0

  • Expected output

    passing 1 iterator instance TF Instance created : 1 passing 1 as DP Test method execution : DP1 Tear Down: 1 passing 2 iterator instance TF Instance created : 2 passing 1 as DP Test method execution : DP1 passing 2 as DP Test method execution : DP2 Tear Down: 2 passing 3 iterator instance TF Instance created : 3 passing 1 as DP Test method execution : DP1 passing 2 as DP Test method execution : DP2 passing 3 as DP Test method execution : DP3 Tear Down: 3 passing 4 iterator instance TF Instance created : 4 passing 1 as DP Test method execution : DP1 passing 2 as DP Test method execution : DP2 passing 3 as DP Test method execution : DP3 passing 4 as DP Test method execution : DP4 Tear Down: 4 [TestNG] Running: Command line suite

    =============================================== Command line suite

    Total tests run: 10, Failures: 0, Skips: 0

Is this a bug in TestNG implementation or lazyloading is not supported for Factory.. TIA

No correct solution

OTHER TIPS

Lazyloading of data provirder is not working for Factory because TestNG is currently invoking all factory methods before running tests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top