Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
leo-13 opened this issue Aug 13, 2013 · 5 comments
Open
Milestone

Comments

@leo-13
Copy link

leo-13 commented Aug 13, 2013

Steps to reproduce:

  • Create a test class MyTest.java:
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);
    }
}
  • Create Iterator class MyIterator.java:
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();
    }
}
  • Run test suite with group-by-instances="true"

Actual result:

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
Copy link
Author

leo-13 commented Aug 13, 2013

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

@adamjson
Copy link

adamjson commented Feb 8, 2016

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

@adamjson
Copy link

adamjson commented Feb 8, 2016

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
Copy link
Member

juherr commented Feb 8, 2016

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
Copy link

+1

This is needed!

@krmahadevan krmahadevan added this to the 7.11.0 milestone Apr 8, 2024
@krmahadevan krmahadevan modified the milestones: 7.11.0, 7.12.0 Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants