简体   繁体   中英

Want to run a custom gradle task only when running Unit test

How to set up gradle to run a particular custom copy task, only when running unit tests?

EDIT

I want to run these tasks when i press build, i. e only in the flavor of the build with unit test execution included.

I've finally found the solution, with the help of this documentation which presents all the tasks that run during build , test , release etc. in a very concise manner. So by making tasks clean , preBuild depend on my copyTask I can ensure that the copy task is run every time the project is cleaned or built.

But since I don't want to run this during building or cleaning process but want to run it when I only run tests, I identified a task that compiles release unit test sources called the compileReleaseUnitTestSources but just mentioning it in build.gradle as

compileReleaseUnitTestSources.dependsOn(myCopyTask)

doesn't actually work, because gradle will give an error saying it cannot find the task compileReleaseUnitTestSources as for some reason that task is not available yet. Instead by enclosing it in a afterEvaluate block we can ensure that this block is executed after all tasks are evaluated, that way we have access to that task now, So finally I added this to my build.gradle

afterEvaluate {
    compileReleaseUnitTestSources.dependsOn(copyResDirectoryToClasses)
}

All the answers here mention to use the dependsOn keyword to attach my task to another task that is run during general build/test execution, but none of them mentioned how to go around the problem where gradle is not able to find the tasks even though you know for sure that these tasks were available and run during build/test execution.

you have to set up a "customCopyTask" and make the "test-task" which does the unittests depend on the "customCopyTask" like this

task customCopyTask(type: Copy) {
    from sourceSets.test.resources
    into sourceSets.test.output.classesDir
}
test.dependsOn customCopyTask

You can make some task finalizing another, in that case this task will run only if another one was called, right after it. This could be done as:

task runUnitTest << {
    println 'running tests'
}

task copyTestResults << {
    println 'copying results'
}

//make copyTestResults finalize runUnitTest
runUnitTest.finalizedBy copyTestResults

You can read about it in the official user guide .

Additionally, if your unit test could be up-to-date and you don't want to run you copy task in that case, you can check the test task status and skip copy-task, as:

task copyTestResults {
    doFirst {
        //chtck anothe task status and skip this one if it didn't actually work
        if (!tasks.getByName("runUnitTest").getState().didWork) {
            throw new StopExecutionException();
        }
    }
    doLast{
        println 'copying results'
    }
}

Or, if you just need to run copy-task before unit tests, make the test task depending on copy-task by setting it's dependsOn property, read about it with a number og examples here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM