简体   繁体   中英

TestNG parallel test/method using dataprovoder

I have a testNG method just like this:

@Test(dataProvider="takeMyProvider")
public void myTest(String param1, String param2){

    System.out.println(param1 + " " + param2);

}

My dataprovider returns 10 elements. My method will be executed 10 times in one thread. How is it possible to parallel this? For example

  • I want to have 5 methods in parallel. The webdriver should open 5 browsers at the same time. After these 5 tests in parallel the other 5 test should be executed

or

  • the webdriver should open 10 browsers and do all 10 elements parallel

Does anybody have an idea?

You can define the parallelism via a suite file in TestNG. Example following runs methods in parallel with 10 threads:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="MySuiteNameHere" parallel="methods" thread-count="10">

    <test name="Selenium Tests">
        <classes>
            <class name="foo.bar.FooTest"/>
        </classes>
    </test>
</suite>

You also need to note that your data provider can is thread safe to allow it to not force the method to run sequentially.

// data providers force single threaded by default
@DataProvider(name = "takeMyProvider", parallel = true)

Be careful, though. TestNG does not create new instances of the class object when running with parallel methods. That means that if you save values on the test class object you can run into threading issues.

Also note, if you set the thread count to 5, it does not wait for the first 5 to all be finished and then start up the next 5. It basically puts all the test methods into a queue and then starts up x threads. Each thread then simply polls the next element from the queue when it is available.

TestNG's @Test annotation already has what you want... To some degree:

// Execute 10 times with a pool of 5 threads
@Test(invocationCount = 10, threadPoolSize = 5)

What this won't do is fit your first scenario exactly, that is, run the first 5, wait for them to finish, run the other 5.

many thx for your feedback and useful tipps. My tests ran - maybe - in any parallel way but only in one browser instance.

Lets jump in in detail:

My dataprovider returns an object[][]

@Dataprovider(name = "takeMyProvider", parallel = true)
public object[][] myProvider(){
    return new object[][]{{"1", "name1"}, {"2", "name2"} {"3", "name3"}}
}

This test method is executed three times

@Test(dataProvider="takeMyProvider")
public void myTest(String param1, String param2){

    System.out.println(param1 + " " + param2);

}

but just in one browser instance. Thats not what I want.

I want testNG to start 3 chrome instances and doing the 3 tests in parallel.

Btw I am running the tests on a selenium grid. Maybe with 100 nodes. It would be perfect when 100 nodes doing this test in parallel. Or even 1.000, depends on the dataprovider.

Does anybody have an idea?

Best regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM