When group-by-instances is set to true the instances created by @Factory does not run in parallel

967 views
Skip to first unread message

Rohit Gupta

unread,
Aug 15, 2016, 5:39:09 PM8/15/16
to testng-users
Hi All,

I am facing a problem when trying to run test cases in parallel with below setup.
On removing group-by-instances="true" from testng xml starts test execution in parallel. But problem with that approach is dependent test will only initiate when all parent tests are completed which i dont want.

Can somebody please help if somebody able to resolve this issue?



package test;

import org.apache.log4j.Logger;
import org.testng.annotations.*;
import org.testng.annotations.Test;

public class TestTest
{
    private String param;

    @Factory( dataProvider = "prov" )
    public TestTest( String param )
    {
        this.param = param;
    }

    @DataProvider( name = "prov" )
    public static Object[][] dataProvider()
    {
        System.out.println( "[" + Thread.currentThread().getId() +  "] Provide data" );
        return new Object[][] {
                { "One" },
                { "Two" },
                { "Three" },
        };
    }

    @BeforeClass
    public void prepare()
    {
        System.out.println( "[" + Thread.currentThread().getId() +  "] Prepare " + param );
    }

    @Test
    public void test1()
    {
        System.out.println( "[" + Thread.currentThread().getId() +  "] Test1 " + param );
    }

    @Test( dependsOnMethods = "test1" )
    public void test2()
    {
        System.out.println( "[" + Thread.currentThread().getId() +  "] Test2 " + param );
        sleep();
    }

    @AfterClass
    public void clean()
    {
        System.out.println( "[" + Thread.currentThread().getId() +  "] Clean " + param );
    }

    private void sleep() {
        try {
            Thread.sleep(10000);
        } catch (Exception ignored) {}
    }
}

With the following testng.xml:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Suite" parallel="instances" thread-count="5" group-by-instances="true">
  <test name="Tests">
    <classes>
       <class name="test.TestTest" />
    </classes>
  </test>
</suite>

⇜Krishnan Mahadevan⇝

unread,
Aug 18, 2016, 10:09:29 PM8/18/16
to testng...@googlegroups.com
Rohit,

I modified your data provider and enabled parallel attribute to true and ran your suite xml with a higher thread count and higher data provider count and I am seeing the instances are running in parallel because they are on different threads. 

public class FactoryInstances {

private String param;

@Factory (dataProvider = "prov")
    public FactoryInstances(String name, int value) {
this.param = name + "(" + value + ")";
System.out.println("[" + Thread.currentThread().getId() + "] instance created with [" + param + "]");
}

@DataProvider (name = "prov", parallel = true)

public static Object[][] dataProvider() {
System.out.println("[" + Thread.currentThread().getId() + "] Provide data");
return new Object[][] {
            {"One", 1},
{"Two", 2},
{"Three",3},

};
}

@BeforeClass
public void prepare() {
System.out.println("[" + Thread.currentThread().getId() + "] Prepare " + param);
}

@Test
public void test1() {
System.out.println("[" + Thread.currentThread().getId() + "] Test1 " + param);
}

@Test (dependsOnMethods = "test1")
public void test2() {
System.out.println("[" + Thread.currentThread().getId() + "] Test2 " + param);
sleep();
}

@AfterClass
public void clean() {
System.out.println("[" + Thread.currentThread().getId() + "] Clean " + param);
}

private void sleep() {
try {
            TimeUnit.SECONDS.sleep(10);
} catch (Exception ignored) {
}
}
}
XML :
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Suite" parallel="instances" thread-count="30" data-provider-thread-count="10" group-by-instances="true"
verbose="2">
<test name="Tests">
<classes>
<class name="organized.chaos.forums.testng.FactoryInstances"/>
</classes>
</test>
</suite>

Output : 
...
... TestNG 6.9.12 by Cédric Beust (ced...@beust.com)
...

[1] Provide data
[1] instance created with  [One(1)]
[1] instance created with  [Two(2)]
[1] instance created with  [Three(3)]
[TestNG] Running:
[TestRunner] Starting executor for test Tests with time out:2147483647 milliseconds.
[11] Prepare Three(3)
[11] Test1 Three(3)
[12] Test2 Three(3)
[12] Clean Three(3)
[13] Prepare One(1)
[13] Test1 One(1)
[14] Test2 One(1)
[14] Clean One(1)
[15] Prepare Two(2)
[15] Test1 Two(2)
[16] Test2 Two(2)
[16] Clean Two(2)
PASSED: test1
PASSED: test2
PASSED: test1
PASSED: test2
PASSED: test1
PASSED: test2

===============================================
    Tests
    Tests run: 6, Failures: 0, Skips: 0
===============================================

===============================================
Suite
Total tests run: 6, Failures: 0, Skips: 0
===============================================


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
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 testng-users+unsubscribe@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

Brandon Dudek

unread,
Jan 27, 2017, 2:16:32 PM1/27/17
to testng-users
It is a known bug: https://github.com/cbeust/testng/issues/326

Please comment on it in GitHub, to get some attention.
Reply all
Reply to author
Forward
0 new messages