MVN测试不遵循使用TestNGDependsOnGroups批注进行编码的测试的执行顺序
我尝试在Eclipse Run As TestNG Test上运行项目。它按预期顺序执行。
package com.chain;
StreamTest.java:
@Test(groups = { "main" })
public class StreamTest {
static int durationBase = 2000;
protected Logger logger = Logger.getLogger(this.getClass().getName());
@BeforeClass()
public void beforeClassMethod() throws InterruptedException {
logger.info("Thread id: " + Thread.currentThread().getId());
Thread.sleep((long) (durationBase * Math.random()));
}
public void testMethod1() throws InterruptedException {
logger.info("Thread id: " + Thread.currentThread().getId());
Thread.sleep((long) (durationBase * Math.random()));
}
public void testMethod2() throws InterruptedException {
logger.info("Thread id: " + Thread.currentThread().getId());
Thread.sleep((long) (durationBase * Math.random()));
}
@AfterClass()
public void afterClassMethod() throws InterruptedException {
logger.info("Thread id: " + Thread.currentThread().getId());
Thread.sleep((long) (durationBase * Math.random()));
}
}
EndStreamTest.java:
package com.chain;
@Test(dependsOnGroups = { "main.*" })
public class EndStreamTest {
static int durationBase = 2000;
protected Logger logger = Logger.getLogger(this.getClass().getName());
@BeforeClass
public void beforeClassMethod() throws InterruptedException {
logger.info("Thread id: " + Thread.currentThread().getId());
Thread.sleep((long) (durationBase * Math.random()));
}
@Test(dependsOnGroups = { "main" }, groups = { "cleanup" })
public void testMethod1() throws InterruptedException {
logger.info("Thread id: " + Thread.currentThread().getId());
Thread.sleep((long) (durationBase * Math.random()));
}
}
testng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="classes" thread-count="10">
<test name="Stonecress Tests">
<packages>
<package name="com.chain" />
</packages>
</test>
</suite>
POM.xml:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<!-- Suite testng xml file to consider for test execution -->
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
预期结果(在Eclipse上运行良好。作为TestNG Test运行项目):
2019年1月28日下午4:59:34 com.chain.StreamTest beforeClassMethod
INFO:线程ID:1
2019年1月28日下午4:59:36 com.chain.StreamTest testMethod1
INFO:线程ID:1
2019年1月28日下午4:59:38 com.chain.StreamTest testMethod2
INFO:线程ID:1
2019年1月28日下午4:59:39 com.chain.StreamTest afterClassMethod
INFO:线程ID:1
2019年1月28日下午4:59:40 com.chain.EndStreamTest beforeClassMethod
INFO:线程ID:1
2019年1月28日下午4:59:40 com.chain.EndStreamTest testMethod1
INFO:线程ID:1
2019年1月28日4:59:42 com.chain.EndStreamTest testMethod2
实际结果(通过mvn测试从命令行运行时):
2019年1月28日下午4:52:41 com.chain.StreamTest beforeClassMethod
INFO:线程ID:15
2019年1月28日4:52:41 com.chain.EndStreamTest beforeClassMethod
INFO:线程ID:18
2019年1月28日4:52:41 com.chain.StreamTest testMethod1
INFO:线程ID:15
2019年1月28日4:52:42 com.chain.EndStreamTest testMethod1
INFO:线程ID:18
2019年1月28日4:52:43 com.chain.EndStreamTest testMethod2
INFO:线程ID:18
2019年1月28日4:52:43 com.chain.StreamTest testMethod2
INFO:线程ID:15
2019年1月28日下午4:52:43 com.chain.StreamTest afterClassMethod
INFO:线程ID:17