Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run unit tests for Android is not on the device or emulator?

In android studio in the new sub-project has, as I understand this integration tests that run on the device or emulator, checks the functionality of the application depends on the SDK android. But I do have methods do not depend on android. Their testing, I would like to spend on the JVM, avoiding run the emulator.

like image 790
georgeci Avatar asked Mar 07 '15 23:03

georgeci


2 Answers

As of version 1.1.0 RC1 of Android Studio and the gradle plugin, you can run JUnit 4 unit tests on a JVM without the need of a device.

Take a look at this post

If you have the latest version of Android Studio (1.1 and above) you do not need to worry about using the correct gradle plugin; it is already done. You still need to set that you want to use the experimental unit testing feature in Settings > Gradle > Experimental.

like image 162
Emmanuel Avatar answered Oct 31 '22 23:10

Emmanuel


I'm actually trying to change my projects with MVP and unit test everything instead of using Espresso for testing the project.

I have a project with 160 tests with espresso and some idling resources and it takes like 20-30 minutes to run, and it turns to be expensive to run tests on aws farm or test droid ($5/run/device), so maybe there´s a hope with MVP and unit tests.

Take a look here: http://tools.android.com/tech-docs/unit-testing-support

  1. You can define a app/src/test/java/<apk_package>/ folder and put all your JUnit 4 tests there and they will run without an emulator

  2. Add the dependency: testCompile 'junit:junit:4.12'

  3. Run tests :D

like image 26
Daniel Gomez Rico Avatar answered Oct 31 '22 23:10

Daniel Gomez Rico