6

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.

georgeci
  • 347
  • 5
  • 16

3 Answers3

8

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.

Emmanuel
  • 13,083
  • 4
  • 39
  • 53
1

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

Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162
0

Just to add, it is important to put the junit test files in the app/src/test/java/[apk_package]/ folder, not the app/src/androidTest/java/[apk_package]/ folder! Putting it in the androidTest folder will force run the android emulator or device runner.

Jamie
  • 397
  • 3
  • 11