问题描述
首先作为信息:我将每个 @Test
放在不同的类中(因此,每个类当然只有1个 @Test
批注)
First as info: I put each @Test
in a different class (so of course each class only has 1 @Test
annotation).
实际上,我的目标是要使用不同的参数重新运行相同的类,但我想先运行另一个类.
Actually my goal is to want to rerun the same class with different parameter, but I want to run another class first beforehand.
我试图找到许多 TestNG 不允许在一个< test>中重复一个类或
.提供的重复代码是一个 @Test
方法注释的引用. invocationCount
函数,我看到了关于 invocationCount
的信息,但是我无法使用 invocationCount
达到我的目标,因为该函数重复了一个 @Test
同时运行,然后我可以运行另一个 @Test
.
I've tried to find many references that TestNG doesn't allow repeat of a class or a @Test
method annotation in one <test>
. The repeat provided is an invocationCount
function, I see about invocationCount
, but I can't achieve my goal with invocationCount
because this function repeats a @Test
at the same time and then I can run another @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()));
}
}
我想像波纹管配置那样运行:
I imagine to run like bellow configuration:
<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
未执行.
But the above configuration didn't go as planned because the second SimpleTest1
was not executed.
然后,我尝试在单独的< test>
中运行,例如波纹管和成功,但我面临着有关每个< test>
的延迟时间的新问题.
Then I tried running it in a separate <test>
like bellow and success, but I'm facing new issue about delay time each <test>
.
依次(而不是并行)运行多个< test>
:
Run multiple <test>
serially (not parallel) as follows:
<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依赖项:
TestNG Maven dependency:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0</version>
<scope>compile</scope>
</dependency>
Surefire:
<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))
IDE : Eclipse (Version: 2018-09 (4.9.0))
操作系统: macOS Mojave(版本10.14.6)
OS : macOS Mojave (Version 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>
以及下一个测试.
But after the first <test>
is finished, there is a delay of about 10 seconds before the next <test>
runs, as well as the next test.
注意:我以为这是IDE的问题(我使用Eclipse),但事实并非如此.我已经尝试通过IDE和命令行以两种方式运行它,并且就此延迟问题给了我相同的结果.
Note: I thought this was a problem with the IDE (I use Eclipse), but it wasn't. I've tried running it in 2 ways, via the IDE and the command line, and give me same result about this delay issue.
使用此命令通过命令行:
Via command line using this command :
mvn clean test -Dsurefire.suiteXmlFiles=testng.xml
是否有任何配置可以减少上述延迟时间?
Is there any configuration to reduce the delay time above?
推荐答案
首先,套件不是测试计划(顺便说一句,可能是一个不错的功能要求),而只是选择测试的一种方式.这意味着您无法定义测试之间的依赖关系.这就是为什么使用相同的测试类不起作用(它应该失败或创建不同的实例)的原因.
First, a suite is not a test plan (could be a good feature request btw) but just a way to select tests. It means you can't define a dependency between tests. That's why having the same test class doesn't work (it should fail or create different instances).
据我所知,最好的方法是将自己的逻辑及其与测试框架的集成分开:
As I understand your needs, the best way is to separate your own logic and its integration with the test framework:
- 根据需要设计您的辅助/夹具类:
_
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()));
}
}
- 定义用于与测试框架集成的类
_
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);
}
}
以及带有预期参数的套件:
And the suite with the expected parameters:
<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,可能是错误的,但您有主意)
(disclaimer: I didn't check the XML against the dtd, could be wrong but you have the idea)
创建装置的命名或方式取决于您自己的约定.
The naming or the way to create fixtures depends on your own conventions.
这篇关于为每个< test>设置延迟时间.在TestNG中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!