You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Github Actions中TestNG测试执行数为0,本地运行正常求排查

问题:Github Actions中TestNG+Maven+Selenium测试显示0/0测试执行完成

本地运行基于TestNG、Maven的Selenium测试完全正常,但配置Github Actions后,测试结果显示0/0测试执行完成,无其他构建错误。

已尝试的操作:

  • 手动安装chromedriver依赖(Selenium 4.x自带该依赖,仍尝试)
  • 将Java版本从22降级至17

当前配置文件

Github Actions配置(actions.yml)

name: Selenium Tests

on:
  push:
    branches:
      - master
      - 38-fix-github-actions-test-run
  pull_request:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-java@v4.2.1
        with:
          distribution: 'zulu'
          java-version: '22'
          cache: 'maven'
      - name: Build with Maven, run tests
        run: mvn -B clean test

Maven配置(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>org.example</groupId>
    <artifactId>java-automation</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>22</maven.compiler.source>
        <maven.compiler.target>22</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.19.1</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.10.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.google.truth</groupId>
            <artifactId>truth</artifactId>
            <version>1.4.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

原因及解决方案

核心原因:Maven未识别到TestNG测试类

Maven默认通过Surefire插件扫描测试类,默认规则是仅识别命名为*Test.java或Test*.java的文件;如果测试类不符合该命名规范,或者未放置在src/test/java目录下,就会出现0测试执行的情况。

具体修复步骤

  1. 检查测试类命名与路径

    • 确保测试类命名符合Maven默认规则(如LoginTest.java或TestLogin.java)
    • 确认测试类都放在src/test/java目录下,且包路径与pom.xml中的groupId对应(例如org.example的测试类需放在src/test/java/org/example/)
  2. 自定义Surefire插件扫描规则(可选)
    如果不想修改测试类命名,可以在pom.xml中添加Surefire插件配置,指定自定义的测试类匹配规则:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.2.5</version>
                <configuration>
                    <includes>
                        <include>**/*Tests.java</include>
                        <include>**/*TestSuite.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
  3. 指定TestNG配置文件(如果有)
    若项目使用自定义testng.xml管理测试套件,需在Maven命令中指定该文件:

    mvn -B clean test -DsuiteXmlFile=src/test/resources/testng.xml
    
  4. 补充Github Actions环境的Chrome安装
    Ubuntu-latest环境默认可能没有Chrome浏览器,需在Actions步骤中添加安装命令:

    - name: Install Chrome
      run: sudo apt-get update && sudo apt-get install -y chromium-browser
    

内容的提问来源于stack exchange,提问作者Eric Camescasse

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 10:57:05