GitLab CI流水线中测试未执行,求排查配置问题
问题描述
我遇到的问题和“Tests don't run in gitlab-ci pipeline”类似,尽管.gitlab-ci.yml和pom.xml配置已尽可能通用,且本地有可正常运行的测试类,但GitLab CI中的测试阶段始终没有执行任何测试。
.gitlab-ci.yml 配置
image: maven:3.8.6-openjdk-18-slim build: stage: build script: - mvn compile tags: - shared_runner1 test: stage: test script: - mvn clean test tags: - shared_runner1
pom.xml 配置
<?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>groupId</groupId> <artifactId>simple-project</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>15</source> <target>15</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.9.0</version> <scope>test</scope> </dependency> </dependencies> </project>
测试执行结果
------------------------------------------------------- T E S T S ------------------------------------------------------- Results : Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:00 min [INFO] Finished at: 2022-09-21T13:39:08Z [INFO] ------------------------------------------------------------------------
问题分析与解决方案
核心问题:缺失JUnit 5测试执行插件
JUnit 5需要maven-surefire-plugin来触发测试执行,你的pom.xml中未配置该插件,导致Maven无法识别并运行测试类。
修复步骤:
- 添加maven-surefire-plugin到pom.xml
在<build><plugins>节点下新增以下配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M7</version> </plugin>
(注:可选择与JUnit 5版本兼容的最新稳定版插件)
确认测试类命名规范
确保测试类符合Maven默认识别规则:类名以Test结尾(如UserServiceTest.java)或以Test开头(如TestUserService.java),否则Maven会跳过该类。统一JDK版本(可选但推荐)
你的GitLab CI镜像使用OpenJDK 18,但pom.xml中指定的编译版本是15,版本不一致可能引发潜在兼容性问题。建议:
- 将CI镜像改为
maven:3.8.6-openjdk-15-slim,或 - 将pom.xml中
<source>和<target>的值改为18
内容的提问来源于stack exchange,提问作者Kookie
相关产品推荐
相关产品推荐

