我正在使用testng运行并行测试,在我的testng java文件中,我有以下代码:
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();
}
}
}
1条答案
按热度按时间r6vfmomb1#
我解决了这个问题: