@Test and @BeforeMethod/@AfterMethod run in different thread?

1,482 views
Skip to first unread message

manishn...@gmail.com

unread,
May 16, 2016, 12:33:56 PM5/16/16
to testng-users
Hi All,

I see that if I have @BeforeMethod @Test and @AfterMethod, and if running in parallel methods, then I see there are 2 thread id - one for @BeforeMethod and @AfterMethod and another for @Test 

Is there a way so that entire flow of related methods - @BeforeMethod @Test and @AfterMethod - are in same thread?

Thanks,
Manish

⇜Krishnan Mahadevan⇝

unread,
May 16, 2016, 11:35:34 PM5/16/16
to testng...@googlegroups.com
Manish,

Do you have a sample which can show this behavior ?

I can't simulate the problem on TestNG 6.9.11. If you look at the output you would notice that for each of the @Test methods [ I have two], the @BeforeMethod and @AfterMethod run on the same thread.

Here's my test class, my xml file and the output.

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class BeforeMethodTestMethodThreadId {
@BeforeMethod
public void beforeMethod() {
printer("beforeMethod");
}

@Test
public void testMethod() {
printer("testMethod");
}

@Test
public void anotherTestMethod() {
printer("anotherTestMethod");
}

@AfterMethod
public void afterMethod() {
printer("afterMethod");
}

private void printer(String methodName) {
String msg = String.format("%s() is running on Thread id [%d]", methodName, Thread.currentThread().getId());
System.err.println("[DEBUGGER] :" + msg);

}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Class1Suite" parallel="methods" verbose="2">
<test name="Class1Test">
<classes>
<class name="organized.chaos.forums.BeforeMethodTestMethodThreadId"/>
</classes>
</test> <!-- Class1Test -->
</suite> <!-- Class1Suite -->

...
... TestNG 6.9.11 by Cédric Beust (ced...@beust.com)
...

[TestNG] Running:
  /Users/krmahadevan/githome/PlayGround/testbed/src/test/resources/before-test-after-method.xml
[TestRunner] Starting executor for test Class1Test with time out:2147483647 milliseconds.

[DEBUGGER] :beforeMethod() is running on Thread id [10]
[DEBUGGER] :beforeMethod() is running on Thread id [9]
[DEBUGGER] :testMethod() is running on Thread id [10]
[DEBUGGER] :anotherTestMethod() is running on Thread id [9]
[DEBUGGER] :afterMethod() is running on Thread id [10]
[DEBUGGER] :afterMethod() is running on Thread id [9]

===============================================
Class1Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================


Process finished with exit code 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...@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.

manishn...@gmail.com

unread,
May 18, 2016, 10:24:34 AM5/18/16
to testng-users
Thanks Krishnan.

I realised that the issue I had was due to timeOut in my suite in testng.xml

Thanks,
manish

Jeff Clyne

unread,
Dec 3, 2016, 1:33:23 AM12/3/16
to testng-users
Thanks for following up on your own post with the solution. You saved me quite the headache!

zoomout

unread,
Jan 2, 2017, 11:58:51 AM1/2/17
to testng-users
It is still a problem in version 6.10. Had to figure it out by myself and spent a lot of time. And only afterwards checked this group for this topic =(

Julien Herr

unread,
Jan 2, 2017, 3:28:49 PM1/2/17
to testng-users
Could be related to https://github.com/cbeust/testng/issues/914
Timeout uses a theadpool aka another new thread. 

er.sunai...@gmail.com

unread,
Apr 5, 2018, 6:02:46 PM4/5/18
to testng-users
Hi but if @Test is disabled will the @BeforeMethod still run?

Krishnan Mahadevan

unread,
Apr 5, 2018, 10:00:20 PM4/5/18
to testng...@googlegroups.com

What happens when you tried doing it?

 

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/

Reply-To: <testng...@googlegroups.com>
Date: Friday, April 6, 2018 at 3:32 AM
To: testng-users <testng...@googlegroups.com>
Subject: [testng-users] Re: @Test and @BeforeMethod/@AfterMethod run in different thread?

--

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


To post to this group, send email to

Reply all
Reply to author
Forward
0 new messages