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

Jenkinsfile执行Robot时无法从Github获取正确test.robot文件

解决Jenkins流水线固定执行旧版Robot测试文件的问题

问题根源

你当前的Jenkinsfile里,Robot测试的执行路径是硬编码的绝对路径:

python -m robot C:/Users/ersha/.jenkins/workspace/Pär_Ershag/Selenium/test.robot

这个路径指向的是固定工作区的旧文件,和你当前选择构建的分支无关——哪怕你设置了"clean before checkout",也只会清理当前分支的工作区,不会改变这个硬编码路径指向的内容。

解决方案

核心是使用相对路径替代绝对路径,让Jenkins调用当前分支checkout出来的测试文件:

  1. 修改Robot执行命令的路径
    把硬编码的绝对路径改成项目仓库内的相对路径,假设test.robot在项目根目录的Selenium文件夹下,命令改成:

    python -m robot Selenium/test.robot
    

    Jenkins流水线默认会在当前分支的工作区根目录执行命令,这样就能调用到对应分支的test.robot文件。

  2. 调整Robot报告的输出路径
    同样把硬编码的outputPath改成相对路径,避免报告写到其他分支的工作区:

    robot outputPath: 'robot-reports', passThreshold: 100.0, unstableThreshold: 70.0, onlyCritical: false
    

    报告会生成在当前分支工作区的robot-reports文件夹下,方便区分不同分支的测试结果。

  3. 验证当前工作区内容(可选)
    可以在Robot执行步骤前加一个调试步骤,确认当前工作区的文件是否正确:

    sh 'pwd' // 打印当前工作目录
    sh 'dir Selenium' // 查看Selenium目录下的文件,确认test.robot存在
    

修改后的完整Jenkinsfile

pipeline {
    agent any
    stages { 
        stage('build'){
           steps{
               sh "mvn compile"
           }
        }
        stage('Test') {
            steps{
                sh "mvn test"
            }
            post {
                always {
                    jacoco(
                        execPattern: 'target/*.exec',
                        classPattern: 'target/classes',
                        sourcePattern: 'src/main/java',
                        exclusionPattern: 'src/test*'
                    )
                    junit '**/TEST*.xml'
                }
            }
        }
        stage('Run Robot and Post Test') {
            steps{
                // 可选:调试用,确认工作区内容
                sh 'pwd'
                sh 'dir Selenium'
                // 执行Robot测试,用相对路径
                sh script: 'python -m robot Selenium/test.robot', returnStatus: true
            }
            post {
                always {
                    // 用相对路径存储测试报告
                    robot outputPath: 'robot-reports', passThreshold: 100.0, unstableThreshold: 70.0, onlyCritical: false
                }
            }
        }
    }
}

额外注意事项

  • 确保你的GitHub仓库中,main和b1分支的Selenium/test.robot确实是不同版本的文件
  • Windows环境下用sh步骤执行命令时,路径分隔符用/是兼容的,无需改成\

内容的提问来源于stack exchange,提问作者Pär Ershag

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 03:37:33