1

Why following code is running tests in different threads? Test class:

public class InstancesOfClass {
    
    public static int counter=0;
    
    public InstancesOfClass() {
        counter++;
    }
    
    @Test
    public void testA() {
        long id=Thread.currentThread().getId();
        System.out.println("A:counter="+counter+"  Thread id:"+id);
    }
    
    @Test
    public void testB() {
        long id=Thread.currentThread().getId();
        System.out.println("B:counter="+counter+"  Thread id:"+id);
    }
    
    @Test
    public void testC() {
        long id=Thread.currentThread().getId();
        System.out.println("C:counter="+counter+"  Thread id:"+id);
    }
    
    
}

testng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="instances">
  <test name="Test1" >
    <classes>
     <class name="InstancesOfClass"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Output:

C:counter=1  Thread id:14
B:counter=1  Thread id:13
A:counter=1  Thread id:12

In the suite I have given parallel="instances". counter=1 shows that there is only one instance of this test class. So why test are executing in different threads despite parallel="instances"?

a Learner
  • 4,944
  • 10
  • 53
  • 89
  • I've found this issue with a lot of discussions https://github.com/cbeust/testng/issues/751 but it mentions grouping this parallel option with method dependencies. You have other case.. It looks like a bug. If it related to the recent testng 7.5 version, you might post it to github. – Max Daroshchanka Jan 21 '22 at 21:12

0 Answers0