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

Jenkins执行Go测试后SonarQube无法识别覆盖率报告文件

解决Go测试覆盖率无法在SonarQube中显示的问题

问题原因

你在Jenkins的src目录下执行go test生成coverage.out,该文件中记录的文件路径是相对于src目录的(比如internal/populate/remove.go),但SonarQube是以Jenkins工作区根目录作为项目根目录扫描的,实际文件路径应为src/internal/populate/remove.go,路径不匹配导致SonarQube无法识别这些文件,进而忽略覆盖率数据。

解决方案

方案1:修改coverage.out的路径前缀

在生成覆盖率报告后,用sed命令给所有文件路径添加src/前缀:

container('golang') {
    script {
        sh '''
        cd src                                                                                 
        go test ./...  -tags test '-coverprofile=coverage.out' '-buildvcs=false'
        sed -i 's/^/src\\//' coverage.out
        '''                                      
    }
}

方案2:在工作区根目录执行go test

直接在Jenkins工作区根目录运行测试,让生成的coverage.out自动包含src/前缀:

container('golang') {
    script {
        sh '''
        go test ./src/...  -tags test '-coverprofile=coverage.out' '-buildvcs=false'
        '''                                      
    }
}

方案3:配置SonarQube的源代码路径

在SonarQube的项目配置(或sonar-project.properties文件)中指定源代码目录为src:

sonar.sources=src

或者在Jenkins的SonarQube扫描步骤中添加参数:

-Dsonar.sources=src

内容的提问来源于stack exchange,提问作者Vishal Kulkarni

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 10:33:21