简体   繁体   中英

TestNg: How to a run particular test in a test suite to repeat n times on different parameters

In the following example, I would like to repeat test4 with different set of parameters on running the testng xml. My intention is to get all tests to run in sequence but the test4 should repeat for some set of parameters that I would like to pass. Is there a way to achieve this?

public class SomeTests(){

@Test
public void test1(){
    ...
}
@Test(priority=1)
public void test2(){
     ...
}
@Test(priority=2)
public void test3(){
    ...
}

@Test(priority=3)
public void test4(String param){
    ...
}

} ```

You can use DataProvider annotation to create a parameter provider and pass it to your test case using dataProvider attribute.

In your case it could be something like this:

...
@DataProvider(name = "myParamProvider")
public Object[][] myParams() {
 return new Object[][] {
   { "param1"},
   { "param2"}
 };
}


@Test(priority=3, dataProvider = "myParamProvider")
public void test4(String param){
    ...
}
...

Or simply by providing the DataProvider method name like this:

...
    @DataProvider
    public Object[][] myParams() {
     return new Object[][] {
       { "param1"},
       { "param2"}
     };
    }
    
    
    @Test(priority=3, dataProvider = "myParams")
    public void test4(String param){
        ...
    }
   ...

Check this out for more: https://testng.org/doc/documentation-main.html#parameters-dataproviders

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