What is the difference between src / androidtest and src / test folders?

The default project in Android Studio has two test folders.

First src/androidTest . This folder already existed in the previous version of Android Studio. However, since some time, by default src/test has a new test folder and a new dependency testCompile 'junit: junit: 4.12' in build.gradle .

My question is: What folder do I use for testing and the differences between them?

+60
android android-studio junit android-testing
Dec 21 '15 at 14:03
source share
2 answers

src/androidTest designed for unit tests that use android hardware.

src/test is for pure unit test that are not related to Android infrastructure. You can run tests here without using a real device or emulator.

You can use both folders. Use the first one to check the code using the Android framework. Use the second to test for code that is pure Java classes. The methods for writing tests are almost the same.

More information here: http://developer.android.com/tools/testing/testing_android.html

+67
Dec 21 '15 at 14:07
source share

An excellent source of information regarding Android testing in general is the developers page. Testing recommendations :

  • Local tests ( /src/test/java/ )

Unit tests that run locally on the Java Virtual Machine (JVM). Use these tests to minimize run time when your tests have no dependencies on the Android platform or when you can mock the dependencies of the Android platform.

  • Benchmark tests ( /src/androidTest/java/ )

Unit tests that run on an Android device or emulator. These tests access Instrumentation information, such as the context of the application you are testing. Use these tests when your tests have Android dependencies that cannot mock objects.

https://developer.android.com/training/testing/start/index.html

+15
Nov 30 '16 at
source share



All Articles