testng-team / testng

TestNG testing framework
https://testng.org
Apache License 2.0
1.99k stars 1.02k forks source link

Lazy initialization doesn't work when using Factory with lazy DataProvider #427

Open leo-13 opened 11 years ago

leo-13 commented 11 years ago

Steps to reproduce:

import org.testng.annotations.*;

public class MyTest {
    private String name;

    @DataProvider
    public static java.util.Iterator generateTestsLazy() {
        return new MyIterator();
    }
    @Factory(dataProvider = "generateTestsLazy")
    public MyTest(String a){
        name = a;
        System.out.println("Creating instance for " + name);
    }

    @BeforeClass
    public void setUp(){
        System.out.println("Setup test: " + name);
    }
    @Test
    public void runTest(){
        System.out.println("Running test: " + name);
    }
}
import java.util.Iterator;

public class MyIterator implements Iterator {
    Object[][] names = {
          new Object[] { "test1"} ,
          new Object[] { "test2"} ,
          new Object[] { "test3"} ,
    };
    private int index = 0;

    public boolean hasNext() {
        return index < names.length;
    }
    public Object next() {
        Object[] result = names[index];
        index++;
        return result;
    }
    public void remove() {
        throw new UnsupportedOperationException();
    }
}

No lazy initialization. All instances are created before the tests are started:

Creating instance for test1 Creating instance for test2 Creating instance for test3 Setup test: test3 Running test: test3 Setup test: test2 Running test: test2 Setup test: test1 Running test: test1

Expected result:

Lazy initialization looking like this: Creating instance for test1 Setup test: test1 Running test: test1 Creating instance for test2 Setup test: test2 Running test: test2 Creating instance for test3 Setup test: test3 Running test: test3

leo-13 commented 11 years ago

Another issue here is that I see first constructor call prior to DataProvider call. Why could this happen?

adamjson commented 8 years ago

I am also running into this issue. Are there any plans to fix this?

adamjson commented 8 years ago

Lazy initialization using an iterator works perfectly when the data provider is specified in a Test annotation, but NOT when it is specified in a Factory annotation.

juherr commented 8 years ago

The current architecture of testng create all instances, collect tests, order them and run them.

To fix this issue, we will have to change a lot, what is not planned yet.

BrandonDudek commented 7 years ago

+1

This is needed!