androidrubydockergradleandroid-testing

How to run a scripts before and after connectedDebugAndroidTest runs


I want to run a bash script from a task (in build.gradle) before the Instrumentation tests starts. That script should run a docker container that contains a ruby bases mock server.

I don't know why I can't get it but this is all I have for now (placed in my build.gradle):

task startMock(type:Exec) {
    println("Executing myScript")
    def proc = "cd ../..".execute()
    proc.waitForProcessOutput(System.out, System.err)

    proc = "../scripts/_mock.sh -a start -p ${projectDir}/../../ -m deps/mock-config".execute()
    proc.waitForProcessOutput(System.out, System.err)
}

gradle.projectsEvaluated {
    connectedDebugAndroidTest.dependsOn startMock
}

The Problem is that the Task runs always, not only on calling connectedDebugAndroidTest (or connectCheck) ...

I'm confused and appreciate any help :) Maybe someone can give me a hint on how to solve this.


Solution

  • OK, I finally got it hoooray :)

    I added the following parts to my build.gradle(app) and now the script is called before and after connectCheckhas been triggered:

    task('mockStart', type: Exec){
        doFirst {
            println "MOCK: Start server ..."
        }
        executable "../../scripts/_mock.sh"
        args '-a', 'start', '-p', "${projectDir}/../../", '-m', 'deps/mock-config'
    }
    
    task('mockStop', type: Exec){
        doFirst {
            println "MOCK: Stop Server ..."
        }
        executable "../../scripts/_mock.sh"
        args '-a', 'stop', '-p', "${projectDir}/../../", '-m', 'deps/mock-config'
    }
    
    gradle.projectsEvaluated {
       connectedDebugAndroidTest.dependsOn mockStart
       connectedDebugAndroidTest.finalizedBy mockStop
    }
    

    Maybe this will help someone that has some error like I had :)

    Good luck & stay tuned!