无法通过Maven运行TestNG脚本问题求助
解决Maven执行TestNG脚本无测试运行的问题
核心问题分析
你的情况主要是Maven Surefire插件配置错误加上命令行参数问题导致测试未执行,结合Spring Boot环境的特性,还有依赖冲突的问题需要处理。
步骤1:修正命令行参数
你原来执行的命令mvn test -Dtest com.example.tests.ScriptTest()有两个错误:
-Dtest参数需要用=连接类名,不能空格- 类名后面不需要加
()
正确的命令应该是:
mvn test -Dtest=com.example.tests.ScriptTest
GitLab CI里的命令参数是对的,但还有其他配置问题需要解决。
步骤2:修正pom.xml的关键配置
你的pom有几个致命问题:
问题A:把Surefire Report插件当成依赖引入了
maven-surefire-report-plugin是Maven插件,不是普通依赖,应该放到<build><plugins>里,而不是<dependencies>。
问题B:未配置Surefire插件使用TestNG
Spring Boot默认用JUnit,需要明确配置Surefire插件使用TestNG。
问题C:TestNG依赖缺失test scope
完整pom里的TestNG依赖没有<scope>test</scope>,会导致编译主代码时也引入TestNG,不符合规范。
问题D:spring-boot-starter-test默认包含JUnit,需要排除
避免JUnit和TestNG的冲突。
修正后的pom.xml关键部分:
<!-- 先处理dependencies部分 --> <dependencies> <!-- 其他依赖保留 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <!-- 排除JUnit,避免和TestNG冲突 --> <exclusions> <exclusion> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> </exclusion> <exclusion> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> </exclusion> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!-- TestNG依赖,加上test scope --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.6.1</version> <scope>test</scope> <exclusions> <exclusion> <groupId>junit</groupId> <artifactId>junit</artifactId> </exclusion> </exclusions> </dependency> <!-- 移除原来的maven-surefire-report-plugin依赖 --> </dependencies> <!-- 然后处理build plugins部分 --> <build> <plugins> <!-- 保留spring-boot-maven-plugin --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> <!-- 添加Surefire插件配置,指定使用TestNG --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.2.1</version> <configuration> <suiteXmlFiles> <!-- 如果有TestNG xml套件可以配置这里,没有的话可以省略,会自动扫描@Test注解 --> <!-- <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile> --> </suiteXmlFiles> <useSystemClassLoader>false</useSystemClassLoader> </configuration> </plugin> <!-- 如果需要生成测试报告,添加surefire-report-plugin作为插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-report-plugin</artifactId> <version>3.2.1</version> <executions> <execution> <phase>test</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
步骤3:确认测试文件位置
TestNG测试类必须放在src/test/java目录下,你的类com.example.tests.ScriptTest对应的路径应该是:
src/test/java/com/example/tests/ScriptTest.java
如果路径不对,Maven会找不到测试类。
步骤4:修正GitLab CI配置
你的artifacts路径写错了,正确的Surefire报告路径是target/surefire-reports,不是sunfire-reports:
image: maven:latest stages: - test test: stage: test script: - mvn test -Dtest=com.example.tests.ScriptTest artifacts: paths: - target/surefire-reports expire_in: 1 week # 可选,设置报告过期时间
验证步骤
- 本地先执行修正后的命令
mvn test -Dtest=com.example.tests.ScriptTest,看控制台是否输出This is a test script,且显示Tests run: 1, Failures: 0 - 推送代码到GitLab,查看CI日志,确认测试执行成功
内容的提问来源于stack exchange,提问作者AppleEater
相关产品推荐
相关产品推荐

