Java 如何在testNG中进行动态并行测试
我正在使用testNG运行并行测试,在我的testNG java文件中,我有以下代码:Java 如何在testNG中进行动态并行测试,java,testng,testng-eclipse,testng-annotation-test,Java,Testng,Testng Eclipse,Testng Annotation Test,我正在使用testNG运行并行测试,在我的testNG java文件中,我有以下代码: public class OfficialTest { @Test public void run1() throws MalformedURLException{ new Controller(1); } @Test public void run2() throws MalformedURLE
public class OfficialTest {
@Test
public void run1() throws MalformedURLException{
new Controller(1);
}
@Test
public void run2() throws MalformedURLException{
new Controller(2);
}
@Test
public void run3() throws MalformedURLException{
new Controller(3);
}
@Test
public void run4() throws MalformedURLException{
new Controller(4);
}
@AfterMethod
public void close() {
System.out.println("closing");
}
}
因此,这将运行4个并行测试,每个测试具有不同的输入。如何才能使其充满活力?我想最终并行测试100个测试,但我不想写100个方法。testNG是否具有此功能
我也尝试过这个,使用线程,这使得测试根本不能并行运行。如有任何建议,将不胜感激
public class OfficialTest extends Thread{
ArrayList<OfficialTest> testThreads = new ArrayList<>();
int row;
ArrayList<ThreadSafeMutableThreadParam> threads = new ArrayList<>();
@Test
public void run1() throws MalformedURLException{
for (int i = 0; i < 4; i++) {
threads.add(i, new ThreadSafeMutableThreadParam(i));
}
for (int i = 0; i < 4; i++) {
ThreadSafeMutableThreadParam t = threads.get(i);
t.run();
}
}
}
class ThreadSafeMutableThreadParam implements Runnable {
private int c;
public ThreadSafeMutableThreadParam( int row ) {
c = row;
}
public synchronized void setC( int c ) {
this.c = c;
}
public synchronized int getC() {
return c;
}
public void run() {
try {
new Controller( getC() );
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public类OfficialTest扩展线程{
ArrayList testThreads=新的ArrayList();
int行;
ArrayList线程=新的ArrayList();
@试验
public void run1()引发格式错误的异常{
对于(int i=0;i<4;i++){
添加(i,新的ThreadSafeMutableThreadParam(i));
}
对于(int i=0;i<4;i++){
ThreadSafeMutableThreadParam t=threads.get(i);
t、 run();
}
}
}
类ThreadSafeMutableThreadParam实现Runnable{
私人INTC;
公共ThreadSafeMutableThreadParam(int行){
c=行;
}
公共同步无效setC(INTC){
这个.c=c;
}
公共同步int-getC(){
返回c;
}
公开募捐{
试一试{
新控制器(getC());
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
我解决了这个问题:
public class ParallelTests
{
int row;
@Parameters({"Row"})
@BeforeMethod()
public void setUp(int rowParam) throws MalformedURLException
{
row = rowParam;
}
@Test
public void RunTest() throws InterruptedException, MalformedURLException
{
new Controller(row);
}
}
我解决了这个问题:
public class ParallelTests
{
int row;
@Parameters({"Row"})
@BeforeMethod()
public void setUp(int rowParam) throws MalformedURLException
{
row = rowParam;
}
@Test
public void RunTest() throws InterruptedException, MalformedURLException
{
new Controller(row);
}
}