Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the fastest way to run unit tests in Android

There are two options available to run unit tests in Android Studio:

  1. Right click on the file with your unit tests (which is Gradle-Aware Make).
  2. testAppDebugUnitTest task is from the list of available Gradle tasks.

The first one actually consists of two tasks: - :app:assembleAppDebug and :app:assembleAppDebugUnitTest

The second one is just testAppDebugUnitTest itself.

The first one is a recommended way by Google Tools Team, but it takes as twice as much time compared to the second one.

There is no visible difference how to run unit tests except the duration. You might think your code won't be compiled without assembleAppDebug, but this is not true - changes you introduce to either class under a test or a test itself compiled and executed as expected.

So, what's the difference and why is it the recommended way if it takes as twice as much time to prepare everything before actual unit tests run?

like image 799
Eugene Avatar asked Aug 12 '15 20:08

Eugene


People also ask

Which framework is more suitable for unit testing Android?

In general, Appium and Calabash are good cross-platform frameworks in testing both your Android and iOS versions at the same time. Espresso is a good choice if you are purely testing Android apps. Therefore, think about your testing need – functional testing, compatibility testing, UI testing, etc.

What should I test in unit tests Android?

Unit tests or small tests only verify a very small portion of the app, such as a method or class. End-to-end tests or big tests verify larger parts of the app at the same time, such as a whole screen or user flow. Medium tests are in between and check the integration between two or more units.


2 Answers

I don't have much work experience with Gradle, But off course it is always better to use Google Team recommended tools instead of others.

it takes as twice as much time compared to the second one.

If you use the new Gradle build system with Android (or Android Studio) you might have realized, that even the simplest Gradle call (e.g. gradle project or grade tasks) is pretty slow.

To avoid this time taking processes in android studio you need to Speed up Gradle build time. It will definitely makes a difference

Here is few steps to speed up the Gradle build time >>

  • In compiler settings (Android Studio -> Preferences -> Project settings[your project] -> Compiler (Gradle-based Android Projects)), type --offline in the "Command-line options" box.

    from ~4 minutes to ~20 seconds.

enter image description here

  • In another way, You can decrease this startup time of Gradle (on my computer down to two seconds), if you tell Gradle to use a daemon to build. Just create a file named gradle.properties in the following directory:
    • /home/<username>/.gradle/ (Linux)
    • /Users/<username>/.gradle/ (Mac)
    • C:\Users\<username>\.gradle (Windows)

Add this line to the file:

org.gradle.daemon=true

From now on Gradle will use a daemon to build, whether you are using Gradle from command line or building in Android Studio. You could also place the gradle.properties file to the root directory of your project and commit it to your SCM system. But you would have to do this, for every project (if you want to use the daemon in every project).

Note: If you don’t build anything with Gradle for some time (currently 3 hours), it will stop the daemon, so that you will experience a long start-up time at the next build.

For more details please refer this Building and running app via Gradle and Android Studio is slower than via Eclipse

Performance improvements are one of the great tasks in the Gradle roadmap for 2015 (and reaching into 2016). So hope, we’ll see the general performance increasing within these years.

n g+ there is a discussion with the developers about it.

Hope this information will helps you.

like image 116
King of Masses Avatar answered Oct 19 '22 06:10

King of Masses


The reason your option 1 takes twice as time is because most of the time is wasted on spinning up Gradle and checking if tasks are up to date. So running an no-op assemble will make it twice as long. You can verify this by running it yourself and check the time wasted by doing an no-op.

With gradle, running with "--offline --daemon --parallel" can get you somewhere (offline is helpful when you have slow internet access to Maven/JCenter repos, daemon is helpful to keep files cached, and parallel is particularly helpful if you have MultiDex on and have split your app into small modules)

If you are just asking about "What's the fastest way to run unit tests in Android?" without considering the change you would need, I would recommend switching to Buck (https://buckbuild.com/), which is 10x faster than Gradle on dex-xing and spinning up tests but has limited AS support.

like image 33
Edison Avatar answered Oct 19 '22 05:10

Edison