在TestNG中为每个<test>设置延迟时间
Set the delay time for each <test> in TestNG
首先作为信息:我把每个@Test
放在不同的class中(所以当然每个class只有1个@Test
注解).
实际上我的目标是想用不同的参数重新运行相同的class,但我想先运行另一个class。
我试图找到许多 TestNG 不允许在一个 [= 中重复 class 或 @Test
方法注释的引用22=]。提供的 repeat 是一个 invocationCount
函数,我看到了 invocationCount
,但是我无法用 invocationCount
实现我的目标,因为这个函数同时重复了一个 @Test
并且然后我可以 运行 另一个 @Test
.
public class SimpleTest1 {
@Test
@Parameters({"acc"})
public void historyTransfer(String acc) {
System.out.println(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(Calendar.getInstance().getTime()));
}
}
public class SimpleTest2 {
@Test
@Parameters({"senderAcc", "beneficiaryAcc", "amount"})
public void tranfer(String senderAcc, String beneficiaryAcc, String amount) {
System.out.println(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(Calendar.getInstance().getTime()));
}
}
我想象 运行 像下面的配置:
<suite name="Suite">
<test name="My Test" >
<classes>
<class name="com.SimpleTest1">
<parameter name="acc" value="11111"></parameter>
</class>
<class name="com.SimpleTest2">
<parameter name="senderAcc" value="11111"></parameter>
<parameter name="beneficiaryAcc" value="22222"></parameter>
<parameter name="amount" value="100"></parameter>
</class>
<class name="com.SimpleTest1">
<parameter name="acc" value="22222"></parameter>
</class>
</classes>
</test>
</suite>
但是上面的配置没有按计划进行,因为第二个SimpleTest1
没有执行
然后我尝试 运行 在单独的 <test>
中使用它,就像下面一样成功,但是我面临着每个 <test>
.
延迟时间的新问题
运行多个<test>
串行(非并行)如下:
<suite name="Suite">
<test name="My Test1" >
<classes>
<class name="com.SimpleTest1">
<parameter name="acc" value="11111"></parameter>
</class>
</classes>
</test>
<test name="My Test2" >
<classes>
<class name="com.SimpleTest2">
<parameter name="senderAcc" value="11111"></parameter>
<parameter name="beneficiaryAcc" value="22222"></parameter>
<parameter name="amount" value="100"></parameter>
</class>
</classes>
</test>
<test name="My Test3" >
<classes>
<class name="com.SimpleTest1">
<parameter name="acc" value="22222"></parameter>
</class>
</classes>
</test>
</suite>
TestNG Maven 依赖项:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0</version>
<scope>compile</scope>
</dependency>
万无一失:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
IDE : Eclipse (版本: 2018-09 (4.9.0))
OS : macOS Mojave(版本 10.14.6)
输出:
[RemoteTestNG] detected TestNG version 7.0.0
22-12-2020 21:59:32
22-12-2020 21:59:47
22-12-2020 21:59:57
===============================================
Suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================
但是在第一个<test>
完成后,在下一个<test>
运行以及下一个测试之前有大约10秒的延迟。
注意:我认为这是 IDE(我使用 Eclipse)的问题,但事实并非如此。我已经通过 IDE 和命令行尝试了 运行 两种方式,并给出了关于这个延迟问题的相同结果。
通过命令行使用此命令:
mvn clean test -Dsurefire.suiteXmlFiles=testng.xml
有什么配置可以减少上面的延迟时间吗?
您可以使用此处所述的注释设置优先级或依赖于其他方法:
https://testng.org/doc/documentation-main.html#annotations
检查@Test.priority 或@Test.dependsOnMethods
这样做,您可以 运行 一个接一个地进行测试。
我不知道为什么你需要在单独的测试中 运行 相同的测试 class,但是当开始测试时它可能会加载所有相关的上下文,这可能需要时间并且可能在测试结束之后可能需要关闭一些资源,两者都可能需要时间/few seconds
您可以verbose使用更多延迟原因的详细信息(并添加更多日志以查看时间)
<suite name="Suite" verbose="10">
The verbosity level is 0 to 10, where 10 is most detailed.
Once you set it to 10, you’ll see that console output will contain information regarding the tests, methods, and listeners, etc.
要加快该过程,您还可以使用 TestNG 的 parallel 功能
The threadPoolSize attribute allows you to specify how many threads should be allocated for this execution.
首先,套件不是测试计划(顺便说一句,这可能是一个很好的功能请求),而只是 select 测试的一种方式。这意味着您不能定义测试之间的依赖关系。这就是为什么相同的测试 class 不起作用(它应该失败或创建不同的实例)。
据我了解您的需求,最好的方法是将您自己的逻辑与其与测试框架的集成分开:
- 根据需要设计您的 helper/fixture class:
_
public class SimpleClass1 {
public void historyTransfer(String acc) {
System.out.println(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(Calendar.getInstance().getTime()));
}
}
public class SimpleClass2 {
public void tranfer(String senderAcc, String beneficiaryAcc, String amount) {
System.out.println(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(Calendar.getInstance().getTime()));
}
}
- 定义 classes 以与测试框架集成
_
public class SimpleTest {
@Test
@Parameters({"acc"})
public void step1(String acc) {
(new SimpleClass1()).historyTransfer(acc);
}
@Test(dependsOnMethods = {"step1"})
@Parameters({"senderAcc", "beneficiaryAcc", "amount"})
public void step2(String senderAcc, String beneficiaryAcc, String amount) {
(new SimpleClass2()).transfer(acc);
}
@Test(dependsOnMethods = {"step2"})
@Parameters({"acc"})
public void step3(String acc) {
(new SimpleClass1()).historyTransfer(acc);
}
}
以及具有预期参数的套件:
<suite name="Suite">
<test name="My Test" >
<classes>
<class name="com.SimpleTest">
<methods>
<include name="step1">
<parameter name="acc" value="11111"></parameter>
</methods>
<methods>
<include name="step2">
<parameter name="senderAcc" value="11111"></parameter>
<parameter name="beneficiaryAcc" value="22222"></parameter>
<parameter name="amount" value="100"></parameter>
</methods>
<methods>
<include name="step3">
<parameter name="acc" value="22222"></parameter>
</methods>
</class>
</classes>
</test>
</suite>
(免责声明:我没有根据 dtd 检查 XML,可能是错误的,但你有想法)
命名或创建灯具的方式取决于您自己的约定。
首先作为信息:我把每个@Test
放在不同的class中(所以当然每个class只有1个@Test
注解).
实际上我的目标是想用不同的参数重新运行相同的class,但我想先运行另一个class。
我试图找到许多 TestNG 不允许在一个 [= 中重复 class 或 @Test
方法注释的引用22=]。提供的 repeat 是一个 invocationCount
函数,我看到了 invocationCount
,但是我无法用 invocationCount
实现我的目标,因为这个函数同时重复了一个 @Test
并且然后我可以 运行 另一个 @Test
.
public class SimpleTest1 {
@Test
@Parameters({"acc"})
public void historyTransfer(String acc) {
System.out.println(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(Calendar.getInstance().getTime()));
}
}
public class SimpleTest2 {
@Test
@Parameters({"senderAcc", "beneficiaryAcc", "amount"})
public void tranfer(String senderAcc, String beneficiaryAcc, String amount) {
System.out.println(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(Calendar.getInstance().getTime()));
}
}
我想象 运行 像下面的配置:
<suite name="Suite">
<test name="My Test" >
<classes>
<class name="com.SimpleTest1">
<parameter name="acc" value="11111"></parameter>
</class>
<class name="com.SimpleTest2">
<parameter name="senderAcc" value="11111"></parameter>
<parameter name="beneficiaryAcc" value="22222"></parameter>
<parameter name="amount" value="100"></parameter>
</class>
<class name="com.SimpleTest1">
<parameter name="acc" value="22222"></parameter>
</class>
</classes>
</test>
</suite>
但是上面的配置没有按计划进行,因为第二个SimpleTest1
没有执行
然后我尝试 运行 在单独的 <test>
中使用它,就像下面一样成功,但是我面临着每个 <test>
.
运行多个<test>
串行(非并行)如下:
<suite name="Suite">
<test name="My Test1" >
<classes>
<class name="com.SimpleTest1">
<parameter name="acc" value="11111"></parameter>
</class>
</classes>
</test>
<test name="My Test2" >
<classes>
<class name="com.SimpleTest2">
<parameter name="senderAcc" value="11111"></parameter>
<parameter name="beneficiaryAcc" value="22222"></parameter>
<parameter name="amount" value="100"></parameter>
</class>
</classes>
</test>
<test name="My Test3" >
<classes>
<class name="com.SimpleTest1">
<parameter name="acc" value="22222"></parameter>
</class>
</classes>
</test>
</suite>
TestNG Maven 依赖项:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0</version>
<scope>compile</scope>
</dependency>
万无一失:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
IDE : Eclipse (版本: 2018-09 (4.9.0))
OS : macOS Mojave(版本 10.14.6)
输出:
[RemoteTestNG] detected TestNG version 7.0.0
22-12-2020 21:59:32
22-12-2020 21:59:47
22-12-2020 21:59:57
===============================================
Suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================
但是在第一个<test>
完成后,在下一个<test>
运行以及下一个测试之前有大约10秒的延迟。
注意:我认为这是 IDE(我使用 Eclipse)的问题,但事实并非如此。我已经通过 IDE 和命令行尝试了 运行 两种方式,并给出了关于这个延迟问题的相同结果。
通过命令行使用此命令:
mvn clean test -Dsurefire.suiteXmlFiles=testng.xml
有什么配置可以减少上面的延迟时间吗?
您可以使用此处所述的注释设置优先级或依赖于其他方法:
https://testng.org/doc/documentation-main.html#annotations
检查@Test.priority 或@Test.dependsOnMethods
这样做,您可以 运行 一个接一个地进行测试。
我不知道为什么你需要在单独的测试中 运行 相同的测试 class,但是当开始测试时它可能会加载所有相关的上下文,这可能需要时间并且可能在测试结束之后可能需要关闭一些资源,两者都可能需要时间/few seconds
您可以verbose使用更多延迟原因的详细信息(并添加更多日志以查看时间)
<suite name="Suite" verbose="10">
The verbosity level is 0 to 10, where 10 is most detailed. Once you set it to 10, you’ll see that console output will contain information regarding the tests, methods, and listeners, etc.
要加快该过程,您还可以使用 TestNG 的 parallel 功能
The threadPoolSize attribute allows you to specify how many threads should be allocated for this execution.
首先,套件不是测试计划(顺便说一句,这可能是一个很好的功能请求),而只是 select 测试的一种方式。这意味着您不能定义测试之间的依赖关系。这就是为什么相同的测试 class 不起作用(它应该失败或创建不同的实例)。
据我了解您的需求,最好的方法是将您自己的逻辑与其与测试框架的集成分开:
- 根据需要设计您的 helper/fixture class:
_
public class SimpleClass1 {
public void historyTransfer(String acc) {
System.out.println(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(Calendar.getInstance().getTime()));
}
}
public class SimpleClass2 {
public void tranfer(String senderAcc, String beneficiaryAcc, String amount) {
System.out.println(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(Calendar.getInstance().getTime()));
}
}
- 定义 classes 以与测试框架集成
_
public class SimpleTest {
@Test
@Parameters({"acc"})
public void step1(String acc) {
(new SimpleClass1()).historyTransfer(acc);
}
@Test(dependsOnMethods = {"step1"})
@Parameters({"senderAcc", "beneficiaryAcc", "amount"})
public void step2(String senderAcc, String beneficiaryAcc, String amount) {
(new SimpleClass2()).transfer(acc);
}
@Test(dependsOnMethods = {"step2"})
@Parameters({"acc"})
public void step3(String acc) {
(new SimpleClass1()).historyTransfer(acc);
}
}
以及具有预期参数的套件:
<suite name="Suite">
<test name="My Test" >
<classes>
<class name="com.SimpleTest">
<methods>
<include name="step1">
<parameter name="acc" value="11111"></parameter>
</methods>
<methods>
<include name="step2">
<parameter name="senderAcc" value="11111"></parameter>
<parameter name="beneficiaryAcc" value="22222"></parameter>
<parameter name="amount" value="100"></parameter>
</methods>
<methods>
<include name="step3">
<parameter name="acc" value="22222"></parameter>
</methods>
</class>
</classes>
</test>
</suite>
(免责声明:我没有根据 dtd 检查 XML,可能是错误的,但你有想法)
命名或创建灯具的方式取决于您自己的约定。