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.

Parallel=instances run tests of same instance on different threads

See original GitHub issue

The documentation says that • parallel=“instances”: TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.

However test methods for the same instances are running on different threads. Please see https://github.com/nmanandhar/TestNGParallelBehaviorTest.git

public class TestClassA {
    private static AtomicInteger instanceCount = new AtomicInteger(0);

    public TestClassA() {
        instanceCount.incrementAndGet();
        log("A new instance of TestClassA created");
    }

    @AfterSuite
    public void printNumberOfInstances() {
        log("Number of instances of TestClassA = " + instanceCount.get());
    }

    @Test
    public void testA1() throws Exception {
        log("testA1 of TestInstance " + this);
    }

    @Test(dependsOnMethods = "testA1")
    public void testB1() {
        log("testB1 of TestInstance " + this);
    }

    @Test(dataProvider = "integerProvider")
    public void testDataProvider(int number) throws Exception {
        log("testDataProvider : number = " + number + " testInstance = " + this);
    }

    @DataProvider
    public Object[][] integerProvider() {
        return new Object[][]{
                {1},
                {2}
        };
    }

    private void log(String message) {
        String threadName = Thread.currentThread().getName();
        synchronized (System.out) {
            System.out.println(String.format("Thread %s : %s ", threadName, message));
        }
    }
}

Suite File

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="InstanceTestSuite" parallel="instances" thread-count="5">
    <test name="InstanceTest">
        <classes>
            <class name="com.nm.TestClassA"/>
        </classes>
    </test>
</suite>

Output of mvn test

Running TestSuite
Thread main : A new instance of TestClassA created
Thread pool-1-thread-1 : testA1 of TestInstance com.nm.TestClassA@58ee2549
Thread pool-1-thread-1 : testDataProvider : number = 1 testInstance = com.nm.TestClassA@58ee2549
Thread pool-1-thread-1 : testDataProvider : number = 2 testInstance = com.nm.TestClassA@58ee2549
Thread pool-1-thread-2 : testB1 of TestInstance com.nm.TestClassA@58ee2549
Thread main : Number of instance = 1
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.835 sec - in TestSuite

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:3
  • Comments:28

github_iconTop GitHub Comments

1reaction
juherrcommented, Sep 29, 2020

@mihalyr @krmahadevan Both of you are right: the feature is working as expected except when a dependency feature is involved. It is a known issue and it should be fixed. But the current implementation won’t allow it easily and nobody has currently enough free time to dig and fix it. I suppose the documentation should be improved with a link to this issue to warn about the issue.

0reactions
mihalyrcommented, Sep 29, 2020

Thanks, was not sure if it affected both parallel instances and classes. Thanks for the links to the code, I’ll try to find some time to look into.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why parallel="instances" running tests within same class ...
counter=1 shows that there is only one instance of this test class. So why test are executing in different threads despite parallel="instances" ...
Read more >
Executing Parallel Tests in TestNG - PawanGaria.com
To Run Test Class parallel meaning each Class will be executed in a different Thread and the same thread is used for Test...
Read more >
Parallel Test Execution in Selenium using TestNG - H2k Infosys
Parallel Testing is a technique where you want to run multiple tests simultaneously in different threads to reduce the execution time.
Read more >
Parallel Test Execution in Selenium using TestNG
parallel = “instances “: TestNG will run all test cases in the same instance in the same thread, but two methods on two...
Read more >
Parallelization with TestNG - Robust Test Automation with Java
instances, TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will...
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