[testng-dev] Lazy initialization doesn't work when using Factory with lazy DataProvider

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[testng-dev] Lazy initialization doesn't work when using Factory with lazy DataProvider

leo.fedorov
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

--
You received this message because you are subscribed to the Google Groups "testng-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/testng-dev.
For more options, visit https://groups.google.com/groups/opt_out.

MyIterator.java (537 bytes) Download Attachment
MyTest.java (587 bytes) Download Attachment
testng.xml (210 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

[testng-dev] Re: Lazy initialization doesn't work when using Factory with lazy DataProvider

Michael Braiman

The same is true when using parallel DataProvider without Factory method. When setting DataProvider parallel flag to true, TestNG iterate through the Iterator and initialize all the objects before the actual execution of the test method. And when parallel flag is set to false, initialization happen as expected before the execution for each object.  I don’t know if this is the same or different issue.



On Wednesday, 21 August 2013 16:51:57 UTC-4, [hidden email] wrote:
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

--
You received this message because you are subscribed to the Google Groups "testng-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/testng-dev.
For more options, visit https://groups.google.com/groups/opt_out.