Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testng executes the @BeforeClass and every @Test at a different thread #1333

Open
1 of 7 tasks
yardening2 opened this issue Feb 14, 2017 · 9 comments
Open
1 of 7 tasks

Comments

@yardening2
Copy link

yardening2 commented Feb 14, 2017

TestNG Version

6.10

Expected behavior

The whole <test>name</test> should run on one thread

Actual behavior

each @test inside the class is executed on a different thread

Is the issue reproductible on runner?

  • Shell
  • Maven
  • Gradle
  • Ant
  • Eclipse
  • IntelliJ
  • NetBeans

Test case sample

So this is the test class. 1 @BeforeClass and 2 @Test:

public class SpacerDesignTest extends SpacerTestLogic {
	
	@BeforeClass
	public void init(){
		createSite();
		super.init();
		addWidgetByAPI(WidgetType.Spacer);
	}
	
	@Test()
	public void changeSpacerHeight(){
		String height = "150px";
		openDesignWindow();
		changeWidgetHeight(height);
		closeEditWindow();
		Assert.assertEquals(getWidgetHeightFromEmulator(), height, "button width shoud be " + height + " but it's " + getWidgetHeightFromEmulator());
	}
	
	@Test()
	public void changeSpacerHeight1(){
		String height = "150px";
		openDesignWindow();
		changeWidgetHeight(height);
		closeEditWindow();
		Assert.assertEquals(getWidgetHeightFromEmulator(), height, "button width shoud be " + height + " but it's " + getWidgetHeightFromEmulator());
	}

}

This is the xml with 3 <test> each with the previous class:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="D1 Tests" verbose="0" parallel="tests" thread-count="10" time-out="7200000">

	<listeners>
    	<listener class-name="com.duda.webdriver.core.SuiteListener" />
    	<listener class-name="com.duda.webdriver.core.TestNGListener" />
	</listeners> 
	
	 <test name="test1">
  	<classes>
  		<class name="com.duda.webdriver.elduderino.tests.widgets.spacer.SpacerDesignTest"></class>
  	</classes>
  	</test>
  	
  	<test name="test2">
  	<classes>
  		<class name="com.duda.webdriver.elduderino.tests.widgets.spacer.SpacerDesignTest"></class>
  	</classes>
  	</test>
  	
  	<test name="test3">
  	<classes>
  		<class name="com.duda.webdriver.elduderino.tests.widgets.spacer.SpacerDesignTest"></class>
  	</classes>
  	</test>
</suite>

This is the log i created with the created threads in every @Test.
As you can see there are more threads created than expected by this example:

Thread is: 1
Thread is: 1
Thread is: 1
Thread is: 1
Thread is: 1
Thread is: 14
Thread is: 14
Thread is: 15
Thread is: 15
Thread is: 16
Thread is: 16
Thread is: 16
Thread is: 14
Thread is: 15
Thread is: 16
Thread is: 14
Thread is: 15
Thread is: 15
Thread is: 14
Thread is: 16
Thread is: 14
Thread is: 16
Thread is: 15
Thread is: 16
Thread is: 15
Thread is: 14
Thread is: 14
Thread is: 14
Thread is: 14
Thread is: 16
Thread is: 16
Thread is: 15
Thread is: 16
Thread is: 15
Thread is: 15
Thread is: 15
Thread is: 15
Thread is: 29
Thread is: 29
Thread is: 29
Thread is: 15
Thread is: 15
Thread is: 15
Thread is: 15
Thread is: 30
Thread is: 30
Thread is: 30
Thread is: 15
Thread is: 15
Thread is: 15
Thread is: 15
Thread is: 14
Thread is: 15
Thread is: 15
Thread is: 14
Thread is: 31
Thread is: 31
Thread is: 31
Thread is: 14
Thread is: 14
Thread is: 14
Thread is: 14
Thread is: 32
Thread is: 32
Thread is: 32
Thread is: 14
Thread is: 14
Thread is: 14
Thread is: 14
Thread is: 16
Thread is: 16
Thread is: 33
Thread is: 33
Thread is: 14
Thread is: 14
Thread is: 33
Thread is: 16
Thread is: 16
Thread is: 16
Thread is: 16
Thread is: 34
Thread is: 34
Thread is: 34
Thread is: 16
Thread is: 16
Thread is: 16
Thread is: 16
Thread is: 16
Thread is: 16
Thread is: 1
Thread is: 1
Thread is: 1
Thread is: 1
Thread is: 1
Thread is: 1
@juherr
Copy link
Member

juherr commented Feb 14, 2017

Duplicate of #1185

Feel free to reopen if you think it is a different issue.

@juherr
Copy link
Member

juherr commented Feb 15, 2017

Comment from #1335 by @yardening2

The issue has nothing to do with dependsOn, it happens with the example test I've attached to issue #1333 And there is no dependsOn there

@yardening2 Ok. Do you think it is a duplicate of #1035 in that case?

@juherr juherr reopened this Feb 15, 2017
@yardening2
Copy link
Author

@juherr
My case isn't dealing with dependsOn nor the @BeforeClass is not the issue.
Every @test in the same class gets executed in a different Thread than the previous @test before it.

In other words, every @test creates a new thread id.
You can see it in the thread numberings i added to my sample.

This happend to me only after updating my TestNG version from 6.9.10 to 6.10.
It affects my logging mechanisms because it relies on every class is a single thread, on all of its @tests

I think when in the testng.xml I specify parallel="tests" not every @test should create a new thread.

@yardening2
Copy link
Author

yardening2 commented Feb 19, 2017

@juherr
Ok, so i found the source to this behavior.

Review this example:

public class Test{
	
	@BeforeClass
	public void init(){
		System.out.println(Thread.currentThread().getId());
	}
	
	@Test
	public void test1(){
		System.out.println(Thread.currentThread().getId());
	}
	
	@Test
	public void test2(){
		System.out.println(Thread.currentThread().getId());
	}

}
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="D1 Tests" verbose="0" parallel="tests" thread-count="10" time-out="7200000">
	
	 <test name="test1">
  	<classes>
  		<class name="Test"></class>
  	</classes>
  	</test>
  	
  	<test name="test2">
  	<classes>
  		<class name="Test"></class>
  	</classes>
  	</test>
  	
  	<test name="test3">
  	<classes>
  		<class name="Test"></class>
  	</classes>
  	</test>
</suite>

I get a different thread id for every method although each tag should be 1 thread.

The reason is the time-out="7200000". When I remove this parameter the tests exectute as expected.

Very strange behavior. Any idea why this happens?

@juherr
Copy link
Member

juherr commented Feb 19, 2017

@yardening2 Sure! The timeout feature is done by a thread pool on its own threads.

I'm pretty sure you issue, or a similar one, is already registered: https://github.com/cbeust/testng/issues?q=is%3Aopen+is%3Aissue+label%3A%22Feature%3A+timeout%22

@spatel-github
Copy link

Hi,
I am a newbie here. I have a related question.

In my case, the @BeforeClass method is executed before every test in the class. The testNG xml file looks like below. I am trying testing on one device for now - but plan is to test on multiple devices at the same time.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="2" configfailurepolicy="continue" name="Parallel Mobile Tests" parallel="tests" preserve-order="true">
  <listeners>
    <listener class-name="listeners.TestListener"/>
    <listener class-name="listeners.CustomReportListener"/>
  </listeners>
  <test name="IPHONE_6SPLUS : Parallel Tests">
    <parameter name="deviceOS" value="iOS"/>
    <parameter name="device" value="IPHONE_6SPLUS"/>
    <classes>
      <!-- <class name="tests.Install"/> -->
      <class name="tests.Class01"/>
      <class name="tests.Class02"/>
      <class name="tests.Class03"/>
      <class name="tests.Class04"/>
      <class name="tests.Class05"/>
     </classes>
  </test> <!-- IPHONE_6SPLUS : Parallel Tests -->
</suite> <!-- Parallel Mobile Tests -->

@selenUser
Copy link

@juherr there will be a fix for this bug soon ?
when running tests in parallel as methods and had a setup on beforeClass, some test not getting started by the "beforeclass".
thanks

@juherr juherr added this to the 7.0 milestone Mar 3, 2019
@krmahadevan
Copy link
Member

@selenUser - Does your scenario involve timeouts ? If not, then please try reproducing this issue using TestNG 7.0.0-beta3 and share a sample reproducible test, if the issue still exists.

@JuliaIskra
Copy link

as well as @BedoreMethod and @AfterMethod

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants