如何使此 testNG 测试动态但保持并行
How can I make this testNG test dynamic but remain parallel
public class FactoryTest {
@Test
@Parameters("Row")
public void run1(int row) throws MalformedURLException{
new Controller(row);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods">
<test thread-count="2" name="factory test" parallel="methods">
<classes>
<class name="RealPackage.FactoryTest">
<methods>
<include name="run1">
<parameter name="Row" value="1"/>
</include>
</methods></class>
</classes>
</test> <!-- OfficialTestName -->
</suite> <!-- Suite -->
这是我需要进行的一项测试的示例 运行。我需要它 运行 与其他测试并行。因此,在测试 run1()
中,我创建了一个 Controller(row)
来启动测试,并将行号传递给它。我想同时 运行 new Controller(1)
和 new Controller(2)
和 new Controller(3)
等。如果我将 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");
}
}
但这不是动态的。我需要能够 运行 使用 row
的任何数字范围。所以我在想也许我可以生成一个 XML 文件来处理这个问题,但我仍然不确定它是否能够以这种方式并行 运行。
我可以用这个修复它:
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>
public class FactoryTest {
@Test
@Parameters("Row")
public void run1(int row) throws MalformedURLException{
new Controller(row);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods">
<test thread-count="2" name="factory test" parallel="methods">
<classes>
<class name="RealPackage.FactoryTest">
<methods>
<include name="run1">
<parameter name="Row" value="1"/>
</include>
</methods></class>
</classes>
</test> <!-- OfficialTestName -->
</suite> <!-- Suite -->
这是我需要进行的一项测试的示例 运行。我需要它 运行 与其他测试并行。因此,在测试 run1()
中,我创建了一个 Controller(row)
来启动测试,并将行号传递给它。我想同时 运行 new Controller(1)
和 new Controller(2)
和 new Controller(3)
等。如果我将 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");
}
}
但这不是动态的。我需要能够 运行 使用 row
的任何数字范围。所以我在想也许我可以生成一个 XML 文件来处理这个问题,但我仍然不确定它是否能够以这种方式并行 运行。
我可以用这个修复它:
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>