Serenity JUnit测试结合LambdaTest Maven Tunnel无法并行执行求助
在Serenity BDD项目中使用LambdaTest网格执行测试时,添加lambdatest-tunnel-binary依赖后,多线程并行测试无法正常运行;但注释掉该依赖后,测试能在LambdaTest控制台正常并行执行。需求是保留LambdaTest Maven Tunnel依赖,同时实现所有测试在LambdaTest网格上的多线程并行执行。
当前项目的POM配置如下:
<?xml version="1.0" encoding="UTF-8"?> <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"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>SerenityBDD</artifactId> <version>1.0-SNAPSHOT</version> <properties> <serenity.version>3.6.12</serenity.version> <serenity.maven.version>3.6.12</serenity.maven.version> <serenity.cucumber.version>3.6.12</serenity.cucumber.version> <maven.compiler.version>3.8.1</maven.compiler.version> <maven.surefire.version>3.0.0-M5</maven.surefire.version> <tags></tags> </properties> <dependencies> <dependency> <groupId>net.serenity-bdd</groupId> <artifactId>serenity-core</artifactId> <version>${serenity.version}</version> </dependency> <dependency> <groupId>net.serenity-bdd</groupId> <artifactId>serenity-junit</artifactId> <version>${serenity.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <dependency> <groupId>net.serenity-bdd</groupId> <artifactId>serenity-rest-assured</artifactId> <version>${serenity.version}</version> </dependency> <dependency> <groupId>net.serenity-bdd</groupId> <artifactId>serenity-cucumber</artifactId> <version>${serenity.cucumber.version}</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.19.0</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.19.0</version> </dependency> <dependency> <groupId>com.github.lambdatest</groupId> <artifactId>lambdatest-tunnel-binary</artifactId> <version>1.0.16</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.version}</version> <configuration> <encoding>UTF-8</encoding> <source>11</source> <target>11</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${maven.surefire.version}</version> <configuration> <includes> <include>**/SerenityTestRunner.java</include> </includes> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <includes> <include>**/SerenityRunner*.java</include> </includes> <parallel>methods</parallel> <threadCount>3</threadCount> </configuration> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>com.diffplug.spotless</groupId> <artifactId>spotless-maven-plugin</artifactId> <version>1.26.0</version> </plugin> <plugin> <groupId>net.serenity-bdd.maven.plugins</groupId> <artifactId>serenity-maven-plugin</artifactId> <version>${serenity.maven.version}</version> <configuration> <tags>${tags}</tags> </configuration> <executions> <execution> <id>serenity-reports</id> <phase>post-integration-test</phase> <goals> <goal>aggregate</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
问题核心是每个测试线程尝试独立启动LambdaTest Tunnel,导致端口冲突或隧道实例重复创建,阻塞并行执行。以下是针对性解决步骤:
1. 调整隧道依赖作用域
将lambdatest-tunnel-binary的依赖作用域设为test,避免污染主类路径,仅在测试阶段可用:
<dependency> <groupId>com.github.lambdatest</groupId> <artifactId>lambdatest-tunnel-binary</artifactId> <version>1.0.16</version> <scope>test</scope> </dependency>
2. 全局控制隧道的启动与关闭
创建测试基类,使用JUnit的@BeforeClass和@AfterClass注解,确保整个测试套件仅启动一次隧道,所有并行测试共享同一实例:
import com.lambdatest.tunnel.Tunnel; import org.junit.AfterClass; import org.junit.BeforeClass; import java.util.HashMap; import java.util.Map; public class LambdaTestTunnelBase { private static Tunnel tunnel; @BeforeClass public static void startTunnel() throws Exception { tunnel = new Tunnel(); Map<String, String> tunnelOptions = new HashMap<>(); tunnelOptions.put("user", "你的LambdaTest用户名"); tunnelOptions.put("key", "你的LambdaTest访问密钥"); // 可添加其他隧道参数,如代理配置等 tunnel.start(tunnelOptions); } @AfterClass public static void stopTunnel() throws Exception { if (tunnel != null && tunnel.isRunning()) { tunnel.stop(); } } }
让所有测试类继承该基类,保证隧道仅在测试开始前启动一次,测试结束后统一关闭。
3. 对齐并行配置
在项目根目录的serenity.properties文件中添加Serenity并行支持配置,与Maven插件的并行策略对齐:
serenity.parallel.enabled=true serenity.parallel.mode=THREAD serenity.parallel.thread.count=3
4. 修复日志依赖冲突
原POM中同时引入slf4j-log4j12和log4j-api/log4j-core会导致日志绑定冲突,影响测试稳定性。移除slf4j-log4j12,改用Log4j2的SLF4J绑定:
<!-- 移除原有冲突依赖 --> <!-- <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> </dependency> --> <!-- 添加Log4j2的SLF4J适配依赖 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.19.0</version> </dependency>
完成修改后,执行以下Maven命令启动测试:
mvn clean verify -Dtags=
测试将通过共享的LambdaTest Tunnel实例,在LambdaTest网格上实现多线程并行执行。
内容的提问来源于stack exchange,提问作者Shahnawaz Sk

