108

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 ?

Kevin ABRIOUX
  • 16,507
  • 12
  • 93
  • 99
  • All of the answers up unto this point have provided an option, but do not utilize the Android Studio test runner (that allows you to jump to source, quickly view results of tests, etc). In order to use those features, you simply need to create a new run configuration that targets the entire project's tests. I have detailed these steps in my answer below: https://stackoverflow.com/a/56192783/2441420 – technoplato May 17 '19 at 20:00
  • you can see the post https://stackoverflow.com/a/71138713/4797289 – Rasoul Miri May 16 '22 at 11:00

7 Answers7

79

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

technoplato
  • 3,293
  • 21
  • 33
  • 2
    Finally - this is literally the most common thing you should do before checking in code! In case it's not clear, it can also be done through the "Edit Configurations..." option from the Run/Debug drop-down combo. I didn't have to change the JRE from Default. I wish there was something similar for Android Instrumented Tests, but they must be created on a per-module basis as they don't seem to support searching the whole project for tests. – Rupert Rawnsley Jun 14 '19 at 10:22
  • 4
    Hi, I'm trying to run Unit Test via command line (for my CI server), it always works with Android Studio, but won't work with command line as it always gives a java.lang.ClassNotFoundException error. After a while, I learned that there's something different. When I run the test from AS, it says Executing tasks: [:lib_view:generateDebugSources, ...] it seems that ./gradlew test skip generateDebugSources task so it can't find the class, do you know what command does AS run when we click run test? – Fadel Trivandi Dipantara Jun 25 '19 at 10:48
  • 3
    This only seems to work for unit tests. What about androidTests? – Adam Johns Aug 14 '20 at 20:23
  • 1
    This really should be built in as a default configuration. – xavierdominguez Oct 23 '20 at 11:07
58

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
And1
  • 710
  • 5
  • 5
  • I just added the task to my app's gradle file and changed the group line to `group = 'Verification'`, thanks for the advice! – davidicus Jul 19 '17 at 20:55
  • 1
    But question is about android studio, he specifically mention one click also so its quite obvious and I am here also because of it. But this answer to run by gradle from command line.. better answer would be, no it's not possible using android studio test runner. – Renetik Jan 11 '19 at 21:24
  • 1
    @Renetik the `testAll` task will appear in the Gradle dialog in Android Studio where you can 'double' click it to run it! Two clicks, I know, but we can let that one slide can't we? – droppin_science Mar 18 '19 at 16:44
  • @Renetik running tests with the Android Studio test runner is possible. In short, you have to create a run configuration that targets all of the Project's tests. Please see my answer for details: https://stackoverflow.com/a/56192783/2441420 – technoplato May 17 '19 at 19:59
  • https://developer.android.com/studio/test/command-line – Saba Jan 07 '22 at 12:18
35

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:))
  • [Seems like this step is no longer required in latest AS version] 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

Benjamin Mesing
  • 4,075
  • 1
  • 18
  • 22
  • 2
    What's `cleanTestDebugUnitTest` for? – mikeD Dec 22 '21 at 10:27
  • 3
    @mikeD cleanTestDebugUnitTest is for cleaning the build first, as far as I know. It is usually added by AS on every Unit test configuration you run manually. (I often remove it to save some time) – Benjamin Mesing Jan 12 '22 at 11:39
22
  1. In "Project" panel (CMD+1 to show) Right click on "java".
  2. Click "Run tests in Java
Johnny Five
  • 987
  • 1
  • 14
  • 29
Andrii Kovalchuk
  • 4,351
  • 2
  • 36
  • 31
12

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

./gradlew test connectedAndroidTest

Windows:

gradlew test connectedAndroidTest
Xavier Bauquet
  • 712
  • 1
  • 6
  • 18
0

enter image description here

If you're having issues running the command from cmd. You can run the command in android studio

  1. Click on Gradle on the right side of Android Studio.

  2. Click on "Execute Gradle Task" icon

  3. Type command "gradle test connectedAndroidTest" and enter

ChinLoong
  • 1,735
  • 24
  • 26
0

Open Android Studio Giraffe | 2022.3.1

  1. Open Gradle panel on the right-top side
  2. Project name -> Tasks -> verification -> click "testDebugUnitTest" (Debug is your variants)