jenkins 如何重试失败的Android仪器测试?

jmp7cifd  于 2023-08-03  发布在  Jenkins
关注(0)|答案(1)|浏览(158)

我正在寻找一种方法来重试运行失败的Android Jmeter 测试或称为集成测试/ui测试。我认为有三种方法可以做到这一点。

  • 使用Jenkins阶段脚本重试->检索所有失败的测试,并使用run gradle命令./gradlew clean app:connectedVariantNameAndroidTest再次执行它们->我还没有尝试这种方法。
  • 插件:我尝试了Gordon,但是,它的最低Java支持版本是17,这与我们的CI机器(Java 11)不一致。
  • 在代码中实现为一些helper类:已尝试此post->检测测试失败时未重试。

我认为在测试自动化中通常使用对插装失败的测试进行重试,因为插装测试是不稳定的,并且容易失败。请帮助如果你有任何方法来实现这一点。

zsohkypk

zsohkypk1#

您可以从任何阶段重新启动声明性管道。如果您保持构建工作区,并且错误出现在管道执行之外,而不是在代码或测试或依赖项中,那么它将起作用。在这种情况下,你需要更新你的来源。在某些情况下,你可能需要使用gradle(dotnet mvn etc ...) clean。如果你的代码必须重新编译,你可以把它放在post{always {}}步骤中。

示例

Jenkins侧

1.简单示例:

pipeline {
    agent {label 'linux'}

    stages {
        stage('Random exit code') {
            steps {
                script{

// random result implementation based on exit code
                    def status = sh(
                                    returnStdout: true,
                                    script:'''
                                            bash -c 'echo $(( $RANDOM % 2 ))'
                                            '''
                                    ).toInteger()
                    print("script status: $status")
                    if (status != 0) {
                      currentBuild.result = 'FAILED'
                    }
                }
            }
        }
    }
    post{
        success{
            echo 'ok'
        }
        failure{
            echo 'fail'
// rebuild same pipeline
            build  wait: false, job: '/dev-env/simple_example'
        }
            
    }
}

字符串
Jenkins邮报
1.使用声明性管道阶段重启的高级示例:
管道:

pipeline {
    agent {label 'linux'}
    stages {
        stage('build') {
            steps {
                sh 'touch testfile'
            }
        }
        stage('parralel tests') {
            parallel{
                stage('unit test'){
                    steps{
                        sh "echo '${STAGE_NAME} stage' >> testfile"
                        sh 'cat testfile'
                    }
                }
                stage('smoke test'){
                    steps{
                        sh "echo '${STAGE_NAME} stage' >> testfile"
                        sh 'cat testfile'
                    }
                }
                stage('postman api test'){
                    steps{
                        sh "echo '${STAGE_NAME} stage' >> testfile"
                        sh 'cat testfile'
                    }
                }
            }
        }
        stage('integration tests') {
            steps {
                sh "echo '${STAGE_NAME} stage' >> testfile"
                sh 'cat testfile'
            }
        }
    }
    
    post{
        success{
            // cleanWs() 
            // cleanWs()  commented becouse this build always success.
            // in real world you need to cleanup your workspace if run is OK  
            sh 'cat testfile'
        }
        failure{
            sh 'cat testfile'
            echo 'something is failed'
        }
            
    }
}


产出:

11:58:49  + echo integration tests stage
11:58:49  [Pipeline] sh
11:58:49  + cat testfile
11:58:49  unit test stage
11:58:49  smoke test stage
11:58:49  postman api test stage
11:58:49  integration tests stage
11:58:49  [Pipeline] }
11:58:49  [Pipeline] // stage
11:58:49  [Pipeline] stage
11:58:49  [Pipeline] { (Declarative: Post Actions)
11:58:49  [Pipeline] sh
11:58:49  + cat testfile
11:58:49  unit test stage
11:58:49  smoke test stage
11:58:49  postman api test stage
11:58:49  integration tests stage <<<<
11:58:49  [Pipeline] }
11:58:49  [Pipeline] // stage
11:58:49  [Pipeline] }
11:58:49  [Pipeline] // node
11:58:49  [Pipeline] End of Pipeline
11:58:50  Notified JIRA that a build has completed.
11:58:50  Finished: SUCCESS


重播阶段:

重放输出:

12:02:32  + echo integration tests stage
12:02:32  [Pipeline] sh
12:02:32  + cat testfile
12:02:32  unit test stage
12:02:32  smoke test stage
12:02:32  postman api test stage
12:02:32  integration tests stage
12:02:32  integration tests stage
12:02:32  [Pipeline] }
12:02:32  [Pipeline] // stage
12:02:32  [Pipeline] stage
12:02:32  [Pipeline] { (Declarative: Post Actions)
12:02:32  [Pipeline] sh
12:02:33  + cat testfile
12:02:33  unit test stage
12:02:33  smoke test stage
12:02:33  postman api test stage
12:02:33  integration tests stage
12:02:33  integration tests stage
12:02:33  [Pipeline] }
12:02:33  [Pipeline] // stage
12:02:33  [Pipeline] }
12:02:33  [Pipeline] // node
12:02:33  [Pipeline] End of Pipeline
12:02:33  Notified JIRA that a build has completed.
12:02:33  Finished: SUCCESS

说明:

  • 查看后期注解 * 您可以看到集成测试阶段在同一个环境中运行。这意味着您不需要重新启动构建阶段进行二进制编译
12:02:33  + cat testfile
12:02:33  unit test stage
12:02:33  smoke test stage
12:02:33  postman api test stage
12:02:33  integration tests stage
12:02:33  integration tests stage <<<<<<<


你可以编译简单的和先进的例子,如果你愿意。但我认为这会矫枉过正

Gradle侧

1.使用gradle test retry plugin
1.您可以在pre&post Package 器中添加一些逻辑来检查测试状态并在某些情况下重放用例

相关问题