Execution order of class instance when using @Factory to create tests

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

Execution order of class instance when using @Factory to create tests

Tatery
Hi,

I'd like to execute my test using @Factory that gets data from @DataProvider and I noticed that test instances created by TestNg are executed in random order.
Test class constructor is invoked in the same order as data from data provider so I would expect that the same order should be in class execution but it seems that classes are invoked in random order.
Is it possible to somehow fix order of test instances to execute?
Have you ever seen something similar? Is it a bug or intended behavior or some configuration is missing in my code?
I use testNg version: 6.10 from maven repo.

Below code example is a simplified version of my original test setup. I provide test data to @DataProvider createTestCmds() from XML file and testng.xml has something like this configurations:
<suite name="Basic tests" thread-count="1" group-by-instances="true">
<test name="basic test" group-by-instances="true">
<classes>
<class name="..." />
</classes>
</suite>

public class TestFactory {
 
private Cmd cmd = null;
 
 
@DataProvider
 
public static Iterator<Object[]> createTestCmds() {
   
ArrayList<Object[]> testData = new ArrayList<Object[]>(9);
   
for(int i = 0; i < 9; i++) {
     
Cmd test = new Cmd();
     test
.setCmd("TEST " + i);
     logger
.info("Command in data provider: {}", test.getText());
     
Object[] ob = new Object[] { test };
     testData
.add(ob);
   
}
   
return testData.iterator();
 
}

 
@Factory(dataProvider = "createTestCmds")
 
public TestFactory(Cmd cmd) {
   
this.cmd = cmd;
   logger
.info("Command in constructor: {}", this.cmd.getText());
 
}

 
@DataProvider
 
//this is not needed however data from providers are nicely added to test log
 public Object[][] cmdProvider() {
   
return new Object[][] { { this.cmd } };
 
}

 
@Test(dataProvider = "cmdProvider")
 
public void sendCmd(Cmd cmd) {
   logger
.info("Command to send: {}", cmd.getText());
 
}
}


Execution log:

Commands order in data provider
 TEST 0
 TEST 1
 TEST 2
 TEST 3
 TEST 4
 TEST 5

Commands order in test factory method (test class constructor):
 TEST 0
 TEST 1
 TEST 2
 TEST 3
 TEST 4
 TEST 5

Commands execution order (each execution produce different order):
 TEST 3
 TEST 2
 TEST 5
 TEST 1
 TEST 4
 TEST 0

--
You received this message because you are subscribed to the Google Groups "testng-users" 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 https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Execution order of class instance when using @Factory to create tests

Tatery
UPDATE:
- also tested with TestNg v 6.11
- tests are executed from maven: mvn clean test

W dniu środa, 5 kwietnia 2017 13:55:40 UTC+2 użytkownik Tatery napisał:
Hi,

I'd like to execute my test using @Factory that gets data from @DataProvider and I noticed that test instances created by TestNg are executed in random order.
Test class constructor is invoked in the same order as data from data provider so I would expect that the same order should be in class execution but it seems that classes are invoked in random order.
Is it possible to somehow fix order of test instances to execute?
Have you ever seen something similar? Is it a bug or intended behavior or some configuration is missing in my code?
I use testNg version: 6.10 from maven repo.

Below code example is a simplified version of my original test setup. I provide test data to @DataProvider createTestCmds() from XML file and testng.xml has something like this configurations:
<suite name="Basic tests" thread-count="1" group-by-instances="true">
<test name="basic test" group-by-instances="true">
<classes>
<class name="..." />
</classes>
</suite>

public class TestFactory {
 
private Cmd cmd = null;
 
 
@DataProvider
 
public static Iterator<Object[]> createTestCmds() {
   
ArrayList<Object[]> testData = new ArrayList<Object[]>(9);
   
for(int i = 0; i < 9; i++) {
     
Cmd test = new Cmd();
     test
.setCmd("TEST " + i);
     logger
.info("Command in data provider: {}", test.getText());
     
Object[] ob = new Object[] { test };
     testData
.add(ob);
   
}
   
return testData.iterator();
 
}

 
@Factory(dataProvider = "createTestCmds")
 
public TestFactory(Cmd cmd) {
   
this.cmd = cmd;
   logger
.info("Command in constructor: {}", this.cmd.getText());
 
}

 
@DataProvider
 
//this is not needed however data from providers are nicely added to test log
 public Object[][] cmdProvider() {
   
return new Object[][] { { this.cmd } };
 
}

 
@Test(dataProvider = "cmdProvider")
 
public void sendCmd(Cmd cmd) {
   logger
.info("Command to send: {}", cmd.getText());
 
}
}


Execution log:

Commands order in data provider
 TEST 0
 TEST 1
 TEST 2
 TEST 3
 TEST 4
 TEST 5

Commands order in test factory method (test class constructor):
 TEST 0
 TEST 1
 TEST 2
 TEST 3
 TEST 4
 TEST 5

Commands execution order (each execution produce different order):
 TEST 3
 TEST 2
 TEST 5
 TEST 1
 TEST 4
 TEST 0

--
You received this message because you are subscribed to the Google Groups "testng-users" 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 https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Loading...