Zachi
Zachi

Reputation: 231

Android Local Unit test with coverage report from command line

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

Upvotes: 14

Views: 11229

Answers (5)

Pavel Ivanov
Pavel Ivanov

Reputation: 19

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.

Upvotes: -1

هيثم
هيثم

Reputation: 1071

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

Upvotes: 0

Nico
Nico

Reputation: 2852

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

Upvotes: 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.

Upvotes: -1

Sielar
Sielar

Reputation: 292

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.

Upvotes: 1

Related Questions