如何在testNG中进行动态并行测试
How to make dynamic parallel tests in testNG
我正在 运行 使用 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();
}
}
}
我解决了这个问题:
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);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite thread-count="5" name="BlogSuite" parallel="tests">
<test name="Test 1">
<parameter name="Row" value="1"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
<test name="Test 2">
<parameter name="Row" value="2"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
<test name="Test 3">
<parameter name="Row" value="3"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
<test name="Test 4">
<parameter name="Row" value="4"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
<test name="Test 5">
<parameter name="Row" value="5"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
</suite>
我正在 运行 使用 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();
}
}
}
我解决了这个问题:
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);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite thread-count="5" name="BlogSuite" parallel="tests">
<test name="Test 1">
<parameter name="Row" value="1"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
<test name="Test 2">
<parameter name="Row" value="2"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
<test name="Test 3">
<parameter name="Row" value="3"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
<test name="Test 4">
<parameter name="Row" value="4"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
<test name="Test 5">
<parameter name="Row" value="5"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
</suite>