Gradle DSL method not found: test()
Asked Answered
P

1

5

Tried to add the following code at the end of my build.gradle file in Android-Studio 1.2 (as advised in this post):

test {
    testLogging {
        events "passed", "skipped", "failed", "standardOut", "standardError"
    }
}

But got:

Error:(40, 0) Gradle DSL method not found: 'test()'
Possible causes:
- The project 'xxxxx' may be using a version of Gradle that does not contain the method.
- The build file may be missing a Gradle plugin.

What did I miss?

Pepin answered 7/7, 2015 at 17:56 Comment(5)
It's android? Shouldn't it be androidTest?Nehemiah
@Opal, Just tried with androidTest, but yields the same bug (except instead of test(), it's androidTest())Pepin
Did you apply the plugin? It sounds like the extension isn't installed.Spake
@BenManes Which plugin? I have apply plugin: 'com.android.application'. I tried to add apply plugin: 'java' and/or apply plugin: 'groovy', but these are incompatible with android apparently.Pepin
Can you post a minimal full build file that shows this error? That would help us debug.Spake
R
16

The gradle documentation: https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html

indicates that the 'test' task is sourced from the java plugin:

apply plugin: 'java' // adds 'test' task

This as you say conflicts with the com.android.application plugin.

Solution

I have finally worked out how to do this. Rather than apply the logging changes to the test tasks (which is only available from java plugin) you can apply it to all tasks of type 'Test' as follows:

//Test Logging
tasks.withType(Test) {
    testLogging {
        events "started", "passed", "skipped", "failed"
    }
}

Now when you run ./gradlew test you should get these events logged as the tests are processed.

Risa answered 28/7, 2015 at 0:0 Comment(2)
You rock bro! Just FYI to future me: Place this outside of any other closure (i.e not inside android{} block)Stirring
@ShubhamChaudhary you can include this in android.testOptions.unitTests.all, then you don't need to filter the tasks first (i.e. leave out the first line).Cannibalism

© 2022 - 2024 — McMap. All rights reserved.