2
votes

I would like to run many tests on testng in parallel using a data provider. I would like a thread to run a test and the next thread to run the next test. How do I do this?

Here is what I tried and it didn't work.

@DataProvider(name = "testList", parallel = true)

in the xml:

suite name="knowledgetest" verbose="5" configfailurepolicy="continue" data-provider-thread-count="10" parallel="methods" thread-count="5"

1

1 Answers

0
votes

Maybe you didn't passed the DataProvider to your constructor.

You need to do something like this in order to run the tests in parallel:

TestNG config:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="TestNG-OddCheckerTest" verbose="1" parallel="methods" thread-count="10" data-provider-thread-count="10">
    <test name="OddCheckerTest">
        <classes>
            <class name="com.mypackage.test.OddCheckerTest" />
        </classes>
    </test>
</suite>

TestClass:

package com.mypackage.test;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

@Test
public class OddCheckerTest {

    private long number;

    @Factory(dataProvider = "numberList")
    public OddCheckerTest(long number) {
        this.number = number;
    }

    @DataProvider(name = "numberList", parallel = true)
    public static Object[][] getNumber() {
        return new Object[][]{
                {1}, {3}, {5}, {77}, {111}, {123}, {287}, {305}, {505}, {607}, {709}, {803}, {805}
        };
    }

    @Test
    public void testForOdd() throws InterruptedException {
        System.out.println(Thread.currentThread().getName() +  ": checking that [ " + number + " ] it's odd!");
        Thread.sleep(1000);
        Assert.assertTrue(number % 2 == 1, "Number [" + number + "] is not odd!");
    }
}

Output: enter image description here