Running single test class in parallel threads via @Factory

1,207 views
Skip to first unread message

coldpizza

unread,
Jun 11, 2010, 11:33:00 AM6/11/10
to testng-users
Hi,

I am using testng-5.12.1 and the <suite parallel="classes"> seems to
not be working in parallel mode.

I have a single test class with several @Test annotations. I have also
a class-level @Tes(sequential=true) annotation because I need the
methods inside the class to be run consequitively in a single thread.

I want to run multiple parallel threads of this class, so I have
created a wrapper @Factory class which returns an array of my test
classes.

public class ThreadGenerator {
@Parameters({"counter"})
@Factory
public Object[] generateTests(int count) {
Object[] tests = new Object[count];
for (int i=0; i < count; i++) {
LoginTest test = new LoginTest(i);
tests[i] = test;
}
return tests;
}
}


The test itself looks like this:

public class LoginTest {
private HttpClient client;

public LoginTest(int count) {
super();
id_count = count;
}

@BeforeClass
public void setUp() {
client = new AMFClient(getConnectionManager());
client.doSSLLogin("http://localhost", "user" + id_count,
"pass");
}
private synchronized MultiThreadedHttpConnectionManager
getConnectionManager() {
if (null == conmanager) {
MultiThreadedHttpConnectionManager conmanager = new
MultiThreadedHttpConnectionManager();
}
return conmanager;
}
@AfterClass
public void cleanUp() {
client.closeConnection();
}

@Test(groups={"amftest"}, alwaysRun=true)
private void sendPostRequest1() {
client.doWork1();
}

@Test(groups={"amftest"}, alwaysRun=true)
private void sendPostRequest2() {
client.doWork2();
}
}

My testng.xml file looks like this:
<suite parallel="classes" thread-count="20" time-out="10000">
<test>
<classes>
<class name="ThreadGenerator" />
</classes>
</test>
</suite>


The tests run single-threaded despite `parallel="classes"` at the
suite-level.

I have also tried to use @Test(sequential=true, threadPoolSize = 20,
invocationCount = 20, timeOut = 1000000000) as the class-level
annotation for LoginTest but it does not have any effect.

Where did I go wrong?

Any help or pointers will be appreciated.

Best,
cp


Cédric Beust ♔

unread,
Jun 11, 2010, 12:02:31 PM6/11/10
to testng...@googlegroups.com
Hi,

I think you misunderstand what parallel="classes" means.  From the documentation:

parallel="classes": TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.

--
Cédric




--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.




--
Cédric

Message has been deleted

coldpizza

unread,
Jun 11, 2010, 12:15:50 PM6/11/10
to testng-users
Yes, that's is exactly what I want: run all the methods in the class
in the same thread, but run multiple instances of that class in
parallel with different parameters, e.g. login name.

My target scenario is to generate multiple connections to a server
under different credentials in order to load test the server. The
test
class represents a typical user session and its methods have to be
run
sequentially. I am trying to use a @Factory in order to generate
multiple sessions in order to simulate concurrent connections to the
server.

So the question is can the instances generated by a @Factory be run
in
parallel, and if so how do I do it?


On Jun 11, 7:02 pm, Cédric Beust ♔ <ced...@beust.com> wrote:
> Hi,
>
> I think you misunderstand what parallel="classes" means.  From the
> documentation:
>
> *parallel="classes"*: TestNG will run all the methods in the same class in
> > testng-users...@googlegroups.com<testng-users%2Bunsubscribe@google­groups.com>
> > .

Cédric Beust ♔

unread,
Jun 11, 2010, 12:24:43 PM6/11/10
to testng...@googlegroups.com
Oh, I see.

What might be happening (I'll have to look at the code) is that this setting acts on the class, not its instances.  If this is the case, then all instances of that class will always run in the same thread.

--
Cédric



On Fri, Jun 11, 2010 at 9:14 AM, coldpizza <vri...@gmail.com> wrote:
Yes, that's is exactly what I want: run all the methods in the class

in the same thread, but run multiple instances of that class in
parallel with different parameters, e.g. login name.

My target scenario is to generate multiple connections to a server
under different credentials in order to load test the server. The test
class represents a typical user session and its methods have to be run
sequentially. I am trying to use a @Factory in order to generate
multiple sessions in order to simulate concurrent connections to the
server.

So the question is can the instances generated by a @Factory be run in
parallel, and if so how do I do it?

I.e. I want to have a single test class

On Jun 11, 7:02 pm, Cédric Beust ♔ <ced...@beust.com> wrote:
> Hi,
>
> I think you misunderstand what parallel="classes" means.  From the
> documentation:
>
> *parallel="classes"*: TestNG will run all the methods in the same class in
> > testng-users...@googlegroups.com<testng-users%2Bunsubscribe@google­groups.com>

> > .
> > For more options, visit this group at
> >http://groups.google.com/group/testng-users?hl=en.
>
> --
> Cédric

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.




--
Cédric

coldpizza

unread,
Jun 11, 2010, 12:54:11 PM6/11/10
to testng-users
Ok, thanks. I would be good to have this working with the @Factory.

Until then, is there an equivalent to the @Factory annotation which
would allow more control over instance creation and execution, for
example by directly using the TestNG object?

i.e. something like:
TestNG testng = new TestNG();
testng.setParallel(true);
testng.setTestClasses(new Class[] { LoginTest.class,
LoginTest.class});

But how do I pass arguments to the class constructor in this case? Are
there any test cases in SVN with examples on dynamic test class
creation and running instances in parallel?

Cédric Beust ♔

unread,
Jun 11, 2010, 12:57:42 PM6/11/10
to testng...@googlegroups.com
On Fri, Jun 11, 2010 at 9:14 AM, coldpizza <vri...@gmail.com> wrote:
Yes, that's is exactly what I want: run all the methods in the class

in the same thread, but run multiple instances of that class in
parallel with different parameters, e.g. login name.

How about parallel="methods" and using sequential=true on the class?

--
Cédric

coldpizza

unread,
Jun 11, 2010, 2:03:46 PM6/11/10
to testng-users
I have set the following on the class:

@Test(threadPoolSize = 20, invocationCount = 10, sequential=true)
public class LoginTest {

@BeforeClass
public synchronized void setUp(){
init();
}

@AfterClass
public void cleanUp() {
clean();
}

@Test(alwaysRun=true)
private void testMethod1() {
callMethod1();
}

@Test(alwaysRun=true)
private void testMethod2() {
callMethod2();
}
}

with the following testng.xml
<suite parallel="methods" thread-count="10" annotations="JDK5">

The result: it still works single-threaded...

What am I missing here?

Cédric Beust ♔

unread,
Jun 11, 2010, 2:19:26 PM6/11/10
to testng...@googlegroups.com
On Fri, Jun 11, 2010 at 11:03 AM, coldpizza <vri...@gmail.com> wrote:
I have set the following on the class:

@Test(threadPoolSize = 20, invocationCount = 10, sequential=true)
public class LoginTest {

   @BeforeClass
   public synchronized void setUp(){
       init();
   }

   @AfterClass
   public  void cleanUp() {
       clean();
   }

   @Test(alwaysRun=true)
   private  void testMethod1() {
       callMethod1();
   }

   @Test(alwaysRun=true)
   private  void testMethod2() {
       callMethod2();
   }
}

with the following testng.xml
<suite parallel="methods" thread-count="10" annotations="JDK5">

The result: it still works single-threaded...

Do you mean that all the methods on all the different instances of the class are running on the same thread?  (are you creating several instances of your class with a factory?)

--
Cédric

coldpizza

unread,
Jun 11, 2010, 2:57:51 PM6/11/10
to testng-users
No, in the last example, I removed the Factory generation, just ran
the class expecting that threadPoolSize = 10 and invocationCount = 10
would be enough. And it only ran once.

I also tried moving all the method's into a single annotated @Method
and then use the invocationCount on it. But in this case failure in
any of the methods would abort all the remaining methods.

Do you mean I should try using @Factory in combination with
threadPoolSize + invocationCount on the class itself. I've tried that
too, and it seems that the class annnotation is simply ignored.

On Jun 11, 9:19 pm, Cédric Beust ♔ <ced...@beust.com> wrote:

Cédric Beust ♔

unread,
Jun 12, 2010, 12:56:32 AM6/12/10
to testng...@googlegroups.com
I have a fix for this, try the beta (http://testng.org/beta), use

<suite parallel="methods">

and

@Test(sequential = true)

at the top of your class.

Then, launch testng with "-Dexperimental=true".

I have to use the experimental flag for now because I will have to introduce a new attribute for this feature (sequentialOnInstances maybe) since changing the behavior of `sequential` might break existing code.

--
Cédric


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.




--
Cédric

coldpizza

unread,
Jun 13, 2010, 6:46:33 AM6/13/10
to testng-users
Cool! Thanks a ton, Cédric! Will try it on Monday and will report on
the results.
> > testng-users...@googlegroups.com<testng-users%2Bunsubscribe@google­groups.com>
> > .

coldpizza

unread,
Jun 17, 2010, 6:54:21 AM6/17/10
to testng-users
Hello Cédric,

Took some time until actually got to test it. The result is: tests
class instances still are executed sequentially. I have used the
latest testng-5.12.2beta.zip and added the 'experimental' flag to the
env settings.

What am I doing wrong?

The actual code I am trying to run is here: http://pastie.org/1008259.

Any comments will be appreciated.

Regards,
coldpiz



On Jun 12, 7:56 am, Cédric Beust ♔ <ced...@beust.com> wrote:
> > testng-users...@googlegroups.com<testng-users%2Bunsubscribe@google­groups.com>
> > .

exu...@uchicago.edu

unread,
Aug 22, 2018, 10:40:37 AM8/22/18
to testng-users
I know this thread is old but I just wanted to revive it. I am having the exact same problem as coldpizza and I still can't find a solution.

Ram Vishwakarma

unread,
Aug 22, 2018, 10:49:15 AM8/22/18
to testng...@googlegroups.com
Hello,

Can anyone suggest me how to refresh native app after each test case, actually I want to run my test case on the home screen of the app.
Please help me I'm new in mobile automation testing.
Currently, I'm using TestNG framework following page object model and for reporting extent report used.

Thanks in advance


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.



--
Regards,
Ramniwas Vishwakarma
Cell No. :- 9977050509

⇜Krishnan Mahadevan⇝

unread,
Aug 22, 2018, 10:51:22 AM8/22/18
to testng...@googlegroups.com
@Ram

Please DONOT hijack threads. Your question is not related to what is being discussed in the current thread.

Also please help keep this forum relevant by posting queries that are related to TestNG ONLY.

Your question question seems to be around perhaps appium. Have you tried posting on the appium forum?
If not, I would suggest you to start there.

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 Scribblings @ http://rationaleemotions.wordpress.com/


To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@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.



--
Regards,
Ramniwas Vishwakarma
Cell No. :- 9977050509

--
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...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages