14

I am working on a new project for Android. Currently, I am using Android studio as IDE. I need to run Unit test and System (CI) test flows which can be run on both Local machine (JVM) and Emulator/Real Device for instrumentation.

Note that I am running all unit tests via command line.

For get the code coverage of the Emulator/Real Device I am using Jacoco. and running the following command: gradlew createDebugCoverageReport However, I can't find any way to run the Local machine unit test with coverage report from command line. The only way is to run it from the android studio by selecting "Run XXX with Code Coverage":

enter image description here

Can you please advise if it is possible to run local unit test from command line with coverage. And get the report as an html file?

Thanks, Zachi

Zachi
  • 231
  • 2
  • 11

5 Answers5

1

If I understood correctly, you are trying to run the tests with coverage ability of the IntelliJ-based Android studio.

It basicly can be done using a Command Line Tool of the IntelliJ. You can read more about it here, but it generally allows you to accomplish everything that can be done from GUI via the command line:

IntelliJ creating command line tools

For more general info regarding the coverage of the IntelliJ tools you can read here:

IntelliJ Code coverage

Hope it helps, good luck.

Sielar
  • 292
  • 2
  • 10
  • I am new to android development. I am using android studio for my development. I want to get JUnit Testing code coverage from command line. Gradle does not support unit testing, so does Jacoco. Is Intellij a pugin that I need to install in android studio? – savi Feb 15 '16 at 18:15
  • 1
    In Android studio you should be able to use gradlew cC command for running unit testing with coverage. It is built in by default in the android studio if I remember correctly, try to read this: https://www.bignerdranch.com/blog/triumph-android-studio-1-2-sneaks-in-full-testing-support/ – Sielar Feb 16 '16 at 12:12
  • Yes gradlew cC command runs tests in androidTest folder. I am trying to run JUnit test cases with code coverage from command line using gradle. – savi Feb 17 '16 at 00:16
  • Though Android Studio is based on IntelliJ, they have differences. I've read the links you provide and didn't give any information related to the question. Please, before to answer anything check that it really answers the question. Otherwise you're making other people to waste their time. – No More Hello World Mar 09 '23 at 09:03
1

At this moment there is no default task to create reporting for Junit (local) tests in the gradle Android tools. However, it's easy to create those.

Just follow the instructions to integrate the custom jacoco.gradle file here:

https://gist.github.com/mrsasha/384a19f97cdeba5b5c2ea55f930fccd4

You will then have tasks like these: test[Flavor]UnitTestCoverage

So to generate the reports, you just have to:

$ ./gradlew test[Flavor]UnitTestCoverage

Nico
  • 2,570
  • 1
  • 9
  • 17
  • thanks for this answer. I checked different pages to configure Jacoco. When I finally run Jacoco from the command line, I found that the code coverage provided by Jacoco differs from the code coverage provided by Android Studio. The difference isn't big. I wonder if I excluded wrong files. I also wonder if anyone else had the same issue. – No More Hello World Mar 09 '23 at 09:00
0

The report can be generated using the android studio, after you run the test with coverage the results window appears, click the button bordered with red

Check this image:

image

هيثم
  • 791
  • 8
  • 11
-1

By running ./gradlew tasks in the terminal if you are using the gradle wrapper or gradle tasks you will have a list of the available verification tasks (see the screenshot below):

enter image description here

You can refer to this link for more thorough test command lines.

  • 2
    This is enabled by adding `testCoverageEnabled = true` to the `debug` element of `buildTypes` in `build.gradle`, but running this task will run and generate coverage for instrumented tests, not unit tests. – Ian Butler Jul 10 '19 at 21:35
  • Bad answer. You only can see this task if you configure the Jacoco plug-in. Otherwise this task is not provided by Gradle. – No More Hello World Mar 09 '23 at 09:05
-1

You can use testDebugUnitTest, run it as ./gradlew <your_module_name>:testDebugUnitTest and it will run tests related to this particular module + it will generate an html report under your_module/build/reports/tests/testDebugUnitTest folder, with the coverage.

  • No, the report doesn't have the test (or code) coverage. It presents the total number of tests, the time lasted to run all of test, the success rate. Please, before putting any answer check that it is right, otherwise you're wasting other people's time. – No More Hello World Mar 09 '23 at 08:53