运行 与 Maven 时测试套件挂起。在 IDEA 中使用 Junit 运行 配置运行良好
Test suite hangs when run with maven. Runs fine with a Junit run configuration in IDEA
我已将我们的项目简化为几个文件,以尝试找出 maven 在套件中间挂起的问题。我在标题中提到的“Junit 运行 配置”只是当您在 intellij IDEA 运行 SmokeTestSuite.java
中创建的默认配置。
SmokeTestSuite.java
package com.parallelTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
})
public class SmokeTestSuite {}
我复制了 Test001 行以模拟进行更多测试。我注意到 256 个测试(每个 Test001 参数化为 8 个测试用例)有时会在 运行 maven 时完成,但是 512 个测试总是挂起,同样 128 个测试永远不会挂起。
Test001.java
package com.parallelTest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.ArrayList;
import java.util.List;
@RunWith(Parameterized.class)
public class Test001 {
private static final Logger logger = LogManager.getLogger(Test001.class);
private static final String[] DRIVER_TYPES;
private static final String[] LANGUAGES;
private static final String[] REGIONS;
private static String userSubmittedServer = System.getProperty("server");
static {
{
DRIVER_TYPES = new String[] {
Constants.BrowserTypes.CHROME,
// Constants.BrowserTypes.FIREFOX,
};
} // DRIVER_TYPES
{
LANGUAGES = new String[] {
Constants.Languages.ENGLISH,
// Constants.Languages.FRENCH,
};
} // LANGUAGES
{
REGIONS = new String[] {
Constants.Regions.AA,
Constants.Regions.BB,
Constants.Regions.CC,
Constants.Regions.DD,
Constants.Regions.EE,
Constants.Regions.FF,
Constants.Regions.GG,
Constants.Regions.HH,
};
} // REGIONS
if (userSubmittedServer == null) {
userSubmittedServer = Constants.DEFAULT_SERVER;
}
else {
userSubmittedServer = userSubmittedServer.toLowerCase();
}
}
public Test001(String browserName, String region, String language) {
logger.info(browserName);
logger.info(region);
logger.info(language);
String server = userSubmittedServer;
logger.info(server);
}
@Parameterized.Parameters(name = "{0}|{1}|{2}")
public static List<String[]> parameterSetup() {
List<String[]> variants = new ArrayList<>();
for (String browserName : DRIVER_TYPES) {
for (String region : REGIONS) {
for (String language : LANGUAGES) {
String[] tempStringList = {browserName, region, language};
variants.add(tempStringList);
}
}
}
return variants;
}
@Test
public void testMethod01() {
logger.info("Test001.testMethod01()");
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>com.parallelTest</groupId>
<artifactId>parallel-test</artifactId>
<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
<executable>C:\Program Files\Java\jdk-12.0.1\bin\javac.exe</executable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<parallel>classes</parallel>
<useUnlimitedThreads>false</useUnlimitedThreads>
<perCoreThreadCount>true</perCoreThreadCount>
<forkCount>1</forkCount>
<threadCount>2</threadCount>
<rerunFailingTestsCount>0</rerunFailingTestsCount>
<parallelTestsTimeoutForcedInSeconds>7200</parallelTestsTimeoutForcedInSeconds>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.0</version>
</dependency>
</dependencies>
</project>
常量文件只是一堆静态最终字符串,它们的值是什么并不重要。当 运行 maven 时,我可以看到测试打印到 app.log,但主进程永远不会结束。任何对此的帮助都会很棒!
编辑:我应该提到这些测试通常是 selenium 测试,每个测试都会产生 chromedriver.exe。所以 运行 无限线程不是一个选项。
问题似乎出在 threadCount
设置中的某处。如果我将 threadCount
替换为 threadCountClasses
,一切都会按预期进行。根据 surefire 文档,我认为我以前的设置也可以。 ::耸肩::
我已将我们的项目简化为几个文件,以尝试找出 maven 在套件中间挂起的问题。我在标题中提到的“Junit 运行 配置”只是当您在 intellij IDEA 运行 SmokeTestSuite.java
中创建的默认配置。
SmokeTestSuite.java
package com.parallelTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
Test001.class,
})
public class SmokeTestSuite {}
我复制了 Test001 行以模拟进行更多测试。我注意到 256 个测试(每个 Test001 参数化为 8 个测试用例)有时会在 运行 maven 时完成,但是 512 个测试总是挂起,同样 128 个测试永远不会挂起。
Test001.java
package com.parallelTest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.ArrayList;
import java.util.List;
@RunWith(Parameterized.class)
public class Test001 {
private static final Logger logger = LogManager.getLogger(Test001.class);
private static final String[] DRIVER_TYPES;
private static final String[] LANGUAGES;
private static final String[] REGIONS;
private static String userSubmittedServer = System.getProperty("server");
static {
{
DRIVER_TYPES = new String[] {
Constants.BrowserTypes.CHROME,
// Constants.BrowserTypes.FIREFOX,
};
} // DRIVER_TYPES
{
LANGUAGES = new String[] {
Constants.Languages.ENGLISH,
// Constants.Languages.FRENCH,
};
} // LANGUAGES
{
REGIONS = new String[] {
Constants.Regions.AA,
Constants.Regions.BB,
Constants.Regions.CC,
Constants.Regions.DD,
Constants.Regions.EE,
Constants.Regions.FF,
Constants.Regions.GG,
Constants.Regions.HH,
};
} // REGIONS
if (userSubmittedServer == null) {
userSubmittedServer = Constants.DEFAULT_SERVER;
}
else {
userSubmittedServer = userSubmittedServer.toLowerCase();
}
}
public Test001(String browserName, String region, String language) {
logger.info(browserName);
logger.info(region);
logger.info(language);
String server = userSubmittedServer;
logger.info(server);
}
@Parameterized.Parameters(name = "{0}|{1}|{2}")
public static List<String[]> parameterSetup() {
List<String[]> variants = new ArrayList<>();
for (String browserName : DRIVER_TYPES) {
for (String region : REGIONS) {
for (String language : LANGUAGES) {
String[] tempStringList = {browserName, region, language};
variants.add(tempStringList);
}
}
}
return variants;
}
@Test
public void testMethod01() {
logger.info("Test001.testMethod01()");
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>com.parallelTest</groupId>
<artifactId>parallel-test</artifactId>
<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
<executable>C:\Program Files\Java\jdk-12.0.1\bin\javac.exe</executable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<parallel>classes</parallel>
<useUnlimitedThreads>false</useUnlimitedThreads>
<perCoreThreadCount>true</perCoreThreadCount>
<forkCount>1</forkCount>
<threadCount>2</threadCount>
<rerunFailingTestsCount>0</rerunFailingTestsCount>
<parallelTestsTimeoutForcedInSeconds>7200</parallelTestsTimeoutForcedInSeconds>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.0</version>
</dependency>
</dependencies>
</project>
常量文件只是一堆静态最终字符串,它们的值是什么并不重要。当 运行 maven 时,我可以看到测试打印到 app.log,但主进程永远不会结束。任何对此的帮助都会很棒!
编辑:我应该提到这些测试通常是 selenium 测试,每个测试都会产生 chromedriver.exe。所以 运行 无限线程不是一个选项。
问题似乎出在 threadCount
设置中的某处。如果我将 threadCount
替换为 threadCountClasses
,一切都会按预期进行。根据 surefire 文档,我认为我以前的设置也可以。 ::耸肩::