Want to run custom gradle task only when Unit test starts

How to configure gradle to run a specific copy task, only when performing unit tests?

EDIT

I want to run these tasks when I click build, i. e only in the flavor of the assembly with unit test running.

+4
source share
3 answers

At last I have found a solution with the help of this document , in which all the tasks that are performed during the build, test, releaseetc. in a very concise form. Therefore, performing tasks clean, preBuilddepends on my copyTask, I can guarantee that the task of copying is started each time the project is built or cleaned.

But since I don’t want to run it during the build or cleanup process, but I want to run it when I run the tests, I defined a task that compiles unit test sources called compileReleaseUnitTestSources, but just mentioning it in build.gradle as

compileReleaseUnitTestSources.dependsOn(myCopyTask)

, gradle , , compileReleaseUnitTestSources, - . , afterEvaluate, , , , . , , build.gradle

afterEvaluate {
    compileReleaseUnitTestSources.dependsOn(copyResDirectoryToClasses)
}

, dependsOn , /, , , gradle , , /.

+2

, , , . :

task runUnitTest << {
    println 'running tests'
}

task copyTestResults << {
    println 'copying results'
}

//make copyTestResults finalize runUnitTest
runUnitTest.finalizedBy copyTestResults

.

, unit test , , :

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'
    }
}

, copy-task , -, dependsOn, og

+1

"customCopyTask" " ", unittests "customCopyTask",

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

Source: https://habr.com/ru/post/1618296/


All Articles