Android command line - how to run a single unit te

2019-08-13 13:22发布

问题:

in android from command line i am trying to run just a single test case. from the IDE it works fine by right click.
But when i try to do it using the following command in CLI IT FAILS:

./gradlew test --tests "com.xyz.b.module.TestClass.testToRun"

i get the following error:

> Unknown command-line option '--tests'.

i guess its a old thread perhaps but i was following here:

Anyway , how can i run my UNIT TEST single method ? i want to emphasis that i want to run a single unit test not instrumentation test from command line.

it should . also be noted my tests are in kotlin and like this:

fun 'i am testing something'(){
//..Arrange
//..Act
//..Assert
}

i guess spaces replaced by underscore right ?

update: i have a camera app. imagine i have a build variant called usCameraDebug. that means united states camera debug. now can you tell me how to run a single test case i called mySingleTest.

i tried this as you mentioned: ./gradlew test --tests "*mySingleTest"

and ./gradlew app:usCameraDebug test --tests "*mySingleTest"

and also: ./gradlew app:usCameraDebugUnitTest --tests "*mySingleTest" but it . does not work. caan you tell me exactly what to type based on my build variant. its in a module called "app" as defaulted.

here is the test i want to run:

package  com.xyz.cameras.parts
    @Test
        fun mySingleTest(){
            assertEquals(12,13)
        }

回答1:

You need to specify a wildcard in the pattern while looking for the test name, and make sure to use module + flavor. --tests will not work with ./gradlew test or ./gradlew check

Try this pattern -> ./gradlew :<module>:<flavor> --tests "*textThatTestNameContains*"

Example -> ./gradlew :profile:testDebug --tests "*my_profile*" will run this test:

@Test
public void my_profile_pageview()

Additionally, running with --info flag helps to see the tests themselves or --debug for more output. e.g. ./gradlew --info :profile:testDebug --tests "*my_profile*"