Android Local Unit test with coverage report from command line
Asked Answered
S

5

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

Sit answered 16/11, 2015 at 15:28 Comment(1)
I've heard about Jacoco, but have no details of it. You should check if it is a possible solution.Adamec
R
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.

Rhiamon answered 18/1, 2016 at 14:21 Comment(4)
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?Arcuate
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: bignerdranch.com/blog/…Rhiamon
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.Arcuate
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.Gombroon
I
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

Instrumental answered 24/1, 2020 at 10:17 Comment(1)
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.Gombroon
O
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

Ovotestis answered 7/7, 2020 at 11:52 Comment(3)
Which directory does the report get output to?Andria
They give you the choice to choose any directory on your deviceSiloam
The op question is about how to do it from the command line. Any answer showing anything in the UI is a waste of time and doesn't answer the question.Gombroon
T
-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.

Thadeus answered 19/10, 2018 at 11:25 Comment(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.Valance
Bad answer. You only can see this task if you configure the Jacoco plug-in. Otherwise this task is not provided by Gradle.Gombroon
I
-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.

Inspan answered 27/7, 2021 at 13:28 Comment(1)
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.Gombroon

© 2022 - 2024 — McMap. All rights reserved.