import java.lang.reflect.Method;  import org.testng.annotations.DataProvider; import org.testng.annotations.Test;  public class NewTest {    @DataProvider(name = "dp")    public Object[][] createData(Method m) {      System.out.println(m.getName());  // print test method name      return new Object[][] { new Object[] { "Cedric" }};     }           @Test(dataProvider = "dp")    public void test1(String s) {           System.out.println(s);         assert true;  }           @Test(dataProvider = "dp")    public void test2(String s) {      System.out.println(s);          assert true;  }    @Test(invocationCount=1000,threadPoolSize=500)  public void testMethod() throws Exception{      int i = 0;   while(i < 100){    System.out.println(i++);    Thread.sleep(100);   }  }  }

invocationCount设定的是这个方法的执行次数

threadPoolSize 这个属性表示的是开启线程数的多少,threadPoolSize的设定要依赖 invocationCount的设定,如果invocationCount的设定值小于threadPoolSize的设定值,多于的设定是无效的,举个极端的例子,如果你threadPoolSize设定是100,而invocationCount没有设定(默认为1次),那么系统只有开启一个线程来运行。反过invocationCount的设定不依赖threadPoolSize,testNG会以默认值1来运行。


开始时我将threadPoolSize设置去掉,然后执行,发现输出结果都是顺序的,而加上threadPoolSize设定后输出开始有些错乱,表明确实是多线程在执行。

我们能够使用这种方法进行并发测试和性能测试。