Run all unit tests in Android Studio

AndroidUnit TestingAndroid StudioAndroid Instrumentation

Android Problem Overview


I have this project in Android Studio :

enter image description here

I wish to run all unit tests in all project with one click.

How i can do it ?

Android Solutions


Solution 1 - Android

Not sure what version of Android Studio this was added in, but I'm using Android Studio 3.4.

Android Studio Version 3.4

In the Project Explorer Window, select the Project View.

Project View Select

Right click on your root project and select "Create 'All Tests'..."

Create 'All Tests'

...this window will appear (these defaults need to be changed)

Default edit config window

Change the following options:

  • Search for tests:

  • In whole project

  • JRE:

  • Most recent version of Android API __ Platform that you have available in this menu (for me, Android API 28 Platform

  • Click Apply

Edited Test configuration

  • Select "All Tests" from the drop down menu

enter image description here

  • View output from all your tests

enter image description here

Solution 2 - Android

First, you can list all the test tasks available in your project with

./gradlew tasks

Then you can choose the tasks you want to execute. If you want to execute all tests for all flavors ans all buildTypes, you just have to run

./gradlew test connectedAndroidTest

If you don't want to remember all the gradle test command each time you want to run the tests, you can create a file "custom_tasks.gradle" and add

task testAll(dependsOn: ['test', 'connectedAndroidTest']) {
   group = 'custom_tasks'
   description = "Run all tests"
}

Then, you just have to run

./gradlew testAll

Solution 3 - Android

With AndroidStudio ArcticFox the approach is as follows:

In the toolbar under the run configuration select "Edit Configurations" Menu entry

Select (+) to add a new configuration and choose Gradle:

Add configuration

On the configuration page:

  • give the configuration a Name (Run all unit tests)
  • Choose your root project as Gradle Project
  • add the Tasks cleanTestDebugUnitTest testDebugUnitTest (you can copy that from an existing configuration for unit tests, but make sure to remove the module prefix (e.g. :app:))
  • add --tests "*" as Arguments (apparently that tells AS to show the results as TestResults)

configuration

Select and run your new configuration. The Results will be shown in the run window: Test run results

Solution 4 - Android

  1. In "Project" panel (CMD+1 to show) Right click on "java".
  2. Click "Run tests in Java

Solution 5 - Android

You can try to use this command line on the android terminal: Mac:

./gradlew test connectedAndroidTest

Windows:

gradlew test connectedAndroidTest

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKevin ABRIOUXView Question on Stackoverflow
Solution 1 - Androidblueberry_chopsticksView Answer on Stackoverflow
Solution 2 - AndroidAnd1View Answer on Stackoverflow
Solution 3 - AndroidBenjamin MesingView Answer on Stackoverflow
Solution 4 - AndroidAndrii KovalchukView Answer on Stackoverflow
Solution 5 - AndroidXavier BauquetView Answer on Stackoverflow