3

I am trying to run just a single Android test case from the command line.

From the IDE I can just right click and run, but from CLI with the following it fails:

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

Error:

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

How can I run a single UNIT TEST method? I want to emphasis that I want to run a single unit test, not an instrumentation test from command line.

Update: I have a camera app. Imagine that 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)
        }
kenny_k
  • 3,831
  • 5
  • 30
  • 41
j2emanue
  • 60,549
  • 65
  • 286
  • 456

1 Answers1

2

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*"

Mark Han
  • 2,785
  • 2
  • 16
  • 31
  • i put a updated . section in my question, can you check. the things you mentioned are not working – j2emanue Aug 01 '19 at 20:07
  • @j2emanue Hey! Check my updated answer. I believe this should work for you. I think you may just need an extra `*` at the end of your test name. Try `./gradlew app:usCameraDebugUnitTest --tests "*mySingleTest*"` I don't think you need a colon before app, but it wouldn't hurt to add it. `./gradlew :app:usCameraDebugUnitTest --tests "*mySingleTest*"` – Mark Han Aug 01 '19 at 21:11
  • 1
    this one works for me: /gradlew :app:usCameraDebugUnitTest --tests "*mySingleTest", thanks – j2emanue Aug 02 '19 at 06:45