How to run a single android unit test using gradle 2.4. Test filtering is not supported

I am using gradle to build my android project and cannot run a single unit test. I have several test classes and one of them MockServerTest

and I only want to run test methods in this class.
I tried to use gradle -Dtest.single=MockServerTest test

, but it turned out that all my tests run, including in other test classes.
I also tried gradle test --tests MockServerTest

but there was an error saying

Test filtering is not supported for given version of junit. Please upgrade junit version to at least 4.6.

      

But I am using junit 4.12 in gradle file

testCompile 'junit:junit:4.12'

      

I am using gradle 2.4 with com.android.tools.build:gradle:1.2.3

. Also, how can I run one test method inside one test class?

By the way, I can run a single test method in AndroidStudio by right clicking on the test method and choosing run targetTestMethod()

from the menu. But how can I achieve this in the terminal? I think AndroidStudio runs a specific command for this as well. How can I see what the command is?

+3


source to share


3 answers


I thought it myself. I need to run

gradle testDebug --tests com.my.package.TestClassName



There are two things here. 1. You must use gradle testDebug

or gradle testRelease

instead of gradle test

. If you have a build variant, you must use gradle testVariantNameDebug

either gradle testVariantNameRelease


2. You must supply the fully qualified class name, including the package name.

+5


source


You can use Android Gradle DSL plugin to set up test issue filters like this:

android {
  testOptions {
    unitTests.all {
      it.testNameIncludePattern = "*.SomeTest"
    }
  }
}

      



You can find more information on testOptions

here and filters here .

+1


source


Have you tried to run gradle test -Dtest.single=MockServerTest

? More information can be found here .

0


source







All Articles