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

使用Jenkins+GitLab无法执行测试的问题求助

问题:Jenkins执行Gradle测试任务无测试执行,Allure报告为空

问题描述

我用IntelliJ IDEA编写测试代码,采用Gradle构建,代码已推送至GitLab。在Jenkins中创建任务并完成如下配置:

  • 源码管理:选择Git,仓库地址为https://gitlab.com/intro4845827/mysecondproject.git,配置用户名密码凭据,分支为master;
  • 构建步骤:调用Gradle脚本,指定Gradle版本,执行Tasks为apiTests;
  • 配置Allure报告。

构建完成后,控制台显示未执行任何测试,Allure报告中也无测试结果。

控制台输出

Started by user Аглиуллин Данил Ильшатович
Running as SYSTEM
Building in workspace /var/jenkins_home/workspace/firstJob
The recommended git tool is: NONE
using credential be1f608d-8fdc-41be-86e1-7fa18ab93947
 > git rev-parse --resolve-git-dir /var/jenkins_home/workspace/firstJob/.git # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://gitlab.com/intro4845827/mysecondproject.git # timeout=10
Fetching upstream changes from https://gitlab.com/intro4845827/mysecondproject.git
 > git --version # timeout=10
 > git --version # 'git version 2.30.2'
using GIT_ASKPASS to set credentials 
 > git fetch --tags --force --progress -- https://gitlab.com/intro4845827/mysecondproject.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse origin/master^{commit} # timeout=10
Checking out Revision 12375e2c77a2d445081ca7d47673e8f1a3c08e18 (origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 12375e2c77a2d445081ca7d47673e8f1a3c08e18 # timeout=10
Commit message: "Правки по тэгам"
 > git rev-list --no-walk 12375e2c77a2d445081ca7d47673e8f1a3c08e18 # timeout=10
[Gradle] - Launching build.
[firstJob] $ /var/jenkins_home/tools/hudson.plugins.gradle.GradleInstallation/Gradle_Jenkins/bin/gradle apiTests
Starting a Gradle Daemon (subsequent builds will be faster)
> Task :compileJava NO-SOURCE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :apiTests

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.2/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD SUCCESSFUL in 37s
2 actionable tasks: 2 executed
Build step 'Invoke Gradle script' changed build result to SUCCESS
[firstJob] $ /var/jenkins_home/tools/ru.yandex.qatools.allure.jenkins.tools.AllureCommandlineInstallation/Allure_Jenkins/bin/allure generate -c -o /var/jenkins_home/workspace/firstJob/allure-report
allure-results does not exist
Report successfully generated to /var/jenkins_home/workspace/firstJob/allure-report
Allure report was successfully generated.
Creating artifact for the build.
Artifact was added to the build.
Finished: SUCCESS

当前build.gradle配置

plugins {
    id 'java'
    id 'io.qameta.allure' version '2.9.6'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
    testImplementation 'junit:junit:4.13.2'

    implementation 'io.qameta.allure:allure-java-commons:2.12.1'
    implementation 'io.qameta.allure:allure-commandline:2.12.1'
    implementation 'io.qameta.allure:allure-rest-assured:2.12.1'
    implementation 'io.qameta.allure:allure-junit5:2.12.1'
    implementation 'io.qameta.allure:allure-assertj:2.12.1'
    implementation 'io.qameta.allure:allure-selenide:2.12.1'
}

def apiTest = tasks.register("apiTests", Test) {
    useJUnitPlatform {
        includeTags "API"
    }
}


test {
    useJUnitPlatform()
}

测试类代码

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Tag;

@Tag("API")
public class MainTest {
    @Test
    public void test1() {
        Assert.assertTrue(false);
    }
}

问题原因

  1. 测试框架注解混用:测试类用了JUnit 4的@Test注解,但标记测试的@Tag是JUnit 5的注解,JUnit 4无法识别@Tag,导致Gradle的apiTests任务按标签过滤时找不到可执行测试。
  2. 依赖配置错误:Allure相关依赖用了implementation而非testImplementation,这些依赖只会在主代码编译阶段加载,测试阶段无法访问,导致Allure无法生成测试结果文件。
  3. 测试任务未关联Allure:apiTests任务没有配置Allure结果输出,测试执行后不会生成allure-results目录,Jenkins的Allure插件找不到结果源文件。

解决方案

1. 统一测试框架注解

将测试类改为使用JUnit 5的完整注解,确保@Tag能被JUnit Platform识别:

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Tag("API")
public class MainTest {
    @Test
    public void test1() {
        Assertions.assertTrue(false);
    }
}

2. 修正依赖配置

将Allure相关依赖从implementation改为testImplementation,确保测试阶段能加载到这些依赖:

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
    // 若无需兼容JUnit 4,可删除该行
    // testImplementation 'junit:junit:4.13.2'

    testImplementation 'io.qameta.allure:allure-java-commons:2.12.1'
    testImplementation 'io.qameta.allure:allure-junit5:2.12.1'
    testImplementation 'io.qameta.allure:allure-assertj:2.12.1'
    testImplementation 'io.qameta.allure:allure-rest-assured:2.12.1'
    testImplementation 'io.qameta.allure:allure-selenide:2.12.1'
}

3. 配置测试任务关联Allure

在apiTests任务中添加Allure相关配置,确保测试执行后生成结果文件:

def apiTest = tasks.register("apiTests", Test) {
    useJUnitPlatform {
        includeTags "API"
    }
    // 让测试任务完成后自动生成Allure结果文件
    finalizedBy(allureReport)
}

4. 可选:清理冗余依赖

如果不需要兼容JUnit 4,直接删除testImplementation 'junit:junit:4.13.2',避免框架冲突。

完成上述修改后,重新推送代码到GitLab,触发Jenkins构建,测试任务会正确执行,Allure报告也会生成对应的测试结果。

内容的提问来源于stack exchange,提问作者Данил Аглиуллин

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 17:14:57