question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DependsOnMethods made parallel class use several threads

See original GitHub issue

TestNG Version

6.9.12 and 6.9.13.8

Expected behavior

When setting TestNG to run tests in parallel by class, all the tests in a single class should be run in the same thread.

Actual behavior

If one of the test method depends on another, a new thread is created, which is really annoying for Selenium case for instance (BeforeClass open the browser, AfterClass kill it)

Test case sample

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="main" parallel="classes" thread-count="2" verbose="0" group-by-instances="false">
    <test name="all">
        <classes>
            <class name="TestFoo"/>
        </classes>
    </test>
</suite>
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestFoo {


  @BeforeClass
  public void navigateTo() {
    System.out.print("BeforeClass - ");
    printThread();
  }

  private void printThread() {
    System.out.println(Thread.currentThread().getName());
  }

  @AfterClass
  public void die() {
    System.out.print("AfterClass - ");
    printThread();
  }

  @Test
  public void t1() {
    System.out.print("T1 - ");
    printThread();
  }

  @Test(dependsOnMethods = "t1")
  public void t2() {
    System.out.print("T2 - ");
    printThread();
  }

}

BeforeClass - pool-1-thread-1
T1 - pool-1-thread-1
T2 - pool-1-thread-2
AfterClass - pool-1-thread-2

What I’ve tried:

  • singleThreaded = true
  • group-by-instance=“true”

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:38 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
juherrcommented, Oct 11, 2016

Yes, the problem is located in GraphThreadPoolExecutor/DynamicGraph. But I don’t know how to fix it for the moment. Don’t expect a quick fix, sorry.

1reaction
mstanclcommented, Jul 12, 2019

I have come across an issue. When i tried this (on both beta7 and beta1) with the VM argument -Dtestng.thread.affinity=true :

import org.testng.annotations.Test;

public class TestClass2 {
    @Test()
    public void test0(){
        System.out.println("TestClass2 - test0. Thread " + Thread.currentThread().getId());
    }

    @Test
    public void test1() {
        System.out.println("TestClass2 - test1. Thread " + Thread.currentThread().getId());
    }

    @Test(dependsOnMethods = "test1")
    public void test2() {
        System.out.println("TestClass2 - test2. Thread " + Thread.currentThread().getId());
    }

    @Test(dependsOnMethods = "test2")
    public void test3() {
        System.out.println("TestClass2 - test3. Thread " + Thread.currentThread().getId());
    }

    @Test()
    public void test4(){
        System.out.println("TestClass2 - test4. Thread " + Thread.currentThread().getId());
    }
}

and then tried to run through xml like this :

<suite name="Default Suite">
    <test  name="testNG tests" parallel="classes" thread-count="50" >
        <classes>
            <class name="ie.kbc.qa.seleniumT24.debugs.TestClass2"/>
        </classes>
    </test>
</suite>

I got an exception :

Exception in thread "TestNG-test=testNG tests-1" java.lang.NullPointerException
TestClass2 - test0. Thread 9
	at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936)
	at org.testng.internal.thread.graph.GraphThreadPoolExecutor.handleThreadAffinity(GraphThreadPoolExecutor.java:156)
	at org.testng.internal.thread.graph.GraphThreadPoolExecutor.afterExecute(GraphThreadPoolExecutor.java:102)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1157)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
TestClass2 - test1. Thread 9
TestClass2 - test4. Thread 9

Process finished with exit code 1

This exception is thrown when there is more then one test with no dependOnMethods

Read more comments on GitHub >

github_iconTop Results From Across the Web

TestNG parallel="classes" and dependsOnMethods
This is open TestNG issue https://github.com/cbeust/testng/issues/1185 parallel="classes" and dependsOnMethods does not work together as ...
Read more >
parallel="methods" + dependsOnMethods() - Google Groups
After C got finished, D runs in the same Thread as C. I hoped, that testNG will use as many Threads as possible,...
Read more >
DependsOnMethods made parallel class use several threads -
When setting TestNG to run tests in parallel by class, all the tests in a single class should be run in the same...
Read more >
Parallel Execution in Selenium: Session Handling & TestNG ...
A new sessionID is created for a new instance of WebDriver. · One session will bind with one particular browser. · Using attribute...
Read more >
Documentation - TestNG
If not set, default mechanism is not to use parallel threads at all. ... The example will make TestNG create two test classes,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found