Đặt thời gian trễ cho mỗi <test> trong TestNG
Đầu tiên là thông tin : Tôi đặt mỗi người @Testtrong một lớp khác nhau (nên tất nhiên mỗi lớp chỉ có 1 @Testchú thích).
Trên thực tế, mục tiêu của tôi là muốn chạy lại cùng một lớp với tham số khác nhau, nhưng tôi muốn chạy trước một lớp khác trước.
Tôi đã cố gắng tìm nhiều tham chiếu mà TestNG không cho phép lặp lại một lớp hoặc một @Testchú thích phương thức trong một <test>. Việc lặp lại được cung cấp là một invocationCounthàm, tôi thấy vậy invocationCount, nhưng tôi không thể đạt được mục tiêu của mình invocationCountvì hàm này lặp lại một @Testhàm cùng một lúc và sau đó tôi có thể chạy một hàm khác @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()));
}
}
Tôi tưởng tượng để chạy như cấu hình dưới đây:
<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>
Nhưng cấu hình trên không diễn ra như kế hoạch vì cấu hình thứ hai SimpleTest1không được thực thi.
Sau đó, tôi đã thử chạy nó một cách riêng biệt <test>như dưới đây và thành công, nhưng tôi đang phải đối mặt với vấn đề mới về thời gian trễ mỗi <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 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 (Version: 2018-09 (4.9.0))
OS : macOS Mojave (Version 10.14.6)
Output:
[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
===============================================
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.
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?
Trả lời
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:
- design your helper/fixture classes as you want:
_
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()));
}
}
- define the classes for the integration with the test framework
_
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>
(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.
You can set priority or dependency on other method using the annotations as described here:
https://testng.org/doc/documentation-main.html#annotations
check for @Test.priority or @Test.dependsOnMethods
Doing this, will allow you run the tests one after the other serially.
I don't know why you need to run same test class in separate tests, but when starting test it may load all relevant context which maybe take time and maybe after test end it may need to close some resources, both may take time/few seconds
You can use verbose to use more details of the cause of the delay (and add more logs to view timing)
<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.
To speed the process you can also use TestNG's parallel feature
The threadPoolSize attribute allows you to specify how many threads should be allocated for this execution.