Reputation: 24771
I want to make sure that a particular gradle task is running before tests only if specific tests is enabled.
For instance, let's say I have test called TranslationTests that look something like this:
@EnabledIfSystemProperty(named = "org.shabunc.tests.TranslationTests", matches = "true")
class TranslationTests {
...
}
Which is activated the following way:
test {
if (project.hasProperty(projectProperty)) {
systemProperty(projectProperty, "org.shabunc.tests.TranslationTests")
}
}
Now I want to be sure that each time I'm running:
gradle test -Porg.shabunc.tests.TranslationTests
before tests some specific gradle task, say gradle prepareTranslationSetup
is triggered. Strictly speaking I want this task to be triggered each time I know TranslationTests are running - and don't be triggered otherwise.
Upvotes: 2
Views: 2456
Reputation: 6770
You can use onlyIf()
on task prepareTranslationSetup
and make test
depend on it. onlyIf()
is defined as follows:
You can use the
onlyIf()
method to attach a predicate to a task. The task’s actions are only executed if the predicate evaluates to true.
(From: Authoring Tasks)
Let's say you've got the following tasks:
task doBeforeTest {
onlyIf {
project.hasProperty("runit")
}
doLast {
println "doBeforeTest()"
}
}
task runTest {
dependsOn = [ doBeforeTest ]
doLast {
println "runTest()"
}
}
doBeforeTest
's actions are only executed if the project has the specified property. runTest
is configured to depend on doBeforeTest
. Now, when you do
gradlew runTest --info
the output is similar to
> Task :doBeforeTest SKIPPED
Skipping task ':doBeforeTest' as task onlyIf is false.
> Task :runTest
runTest()
As you can see doBeforeTest
is skipped as the precondition is not fulfilled. On the other hand, running
gradlew runTest -Prunit
executes doBeforeTest
as expected
> Task :doBeforeTest
doBeforeTest()
> Task :runTest
runTest()
Upvotes: 1