64
votes

I have a test suite for my Android app, and all unit tests run fine. However, whenever I make a single change in one of my unit test classes (for example, ModelUnitTests), when trying to run that class again, I get this message

Process finished with exit code 1
Class not found: "xxx.xxxxxx.xxx.ModelUnitTests"Empty test suite.

If I do a gradle clean and then run the class tests again, it runs fine (but it takes 4 minutes to do...), but then a new change will break it again.

Any advice on how to fix this? I'm not exactly sure which test configuration should I post. I'm using the Unit Tests artifact and my tests are located on the module/src/test/package folder

13
If you are using Robolectric, check if the test file is in the $MODULE$/test directory instead of $MODULE$/androidTest It happened to me, I spent about one hour to figured it outcrgarridos
I have a similar problem but it only happens once after opening Android studio. But all I have to do is try to run the test a second time. Then it runs and continues to run until I have to restart Android studio. Something is not right but the annoyance has been little enough that I have ignored it to date.Brian Reinhold

13 Answers

85
votes

I had a similar problem and it was because I first created an Unit Test with the same class name. When I created the Instrumented Unit Test I got the error.

To solve it, I went to Edit Configurations, on the left of the run icon. Then below Unit Test, it was the 'conflicting' class, which I deleted. Click on Apply/Ok. Then I right click on the class name, click on run and voilà, it works.

22
votes

The fix on Android Studio is:

  • step 1.- Go to Run/Debug configuration
  • step 2.- Go to Android Tests section
  • step 3.- Remove the test configuration file with (-)
  • step 4.- Press Apply and OK
  • step 5.- Run the test again
16
votes

Just ran into this - writing my unit tests in Kotlin. In my case it turned out I forgot to add kotlin plugin in given modules build.gradle file:

apply plugin: 'kotlin-android'
15
votes

I had the same problem. I noticed that the method under test was being displayed in the Run/Debug configuration drop-down as:
TestClassName.testMethod()
rather than the correct:
testMethod()

I fixed it by deleting the TestClass.testMethod() Run/Debug configuration for the test method which was giving this error, then re-running the test.

If that recreates the same problem, delete the incorrect Run/Debug configuration, then right-click on the test method and select:
Create 'testMethod()'...
(rather than Run or Debug) to create a working configuration.

5
votes

I had this problem, and none of the answers on this post (or the other highly-visible Stack Overflow posts) resolved it for me.
However, manually running the gradle task compileTestKotlin appears to have resolved the issue for me.

This was for Kotlin tests, Android Studio 3.1.2

3
votes

If you are working on a team, check all your build.gradle files to make sure nobody is disabling the test tasks. I had the 'empty test suite' error and eventually found it was caused by the following in build.gradle at the project root:

gradle.taskGraph.whenReady {
    tasks.each { task ->
        if (task.name.contains("Test"))
        {
            task.enabled = false
        }
    }
}
2
votes

If you use Robolectric you may need to set Working directory in run configuration as $MODULE_DIR$enter image description here

Also set VM Options : -ea or: -noverify

http://robolectric.org/getting-started/

0
votes

Happened to me in AS 3.3.

I'm using flavors and this happened in a module which only has src/main and src/test. The app module has src/main src/common and src/flavor. The build type selected in AS was flavorDebug.

To fix it I went to "Run Configurations" and in the "Use classpath of module" dropdown the app module was selected. Select the module that you would like to test and voila!

0
votes

Work for me:

  • Build > Clean Project
  • Run test
0
votes

For me finally worked:

  1. ./gradlew check - checked all the errors and tried to fix as many of them.
  2. Restarted Android Studio
  3. Created a new configuration for Android JUnit, test kind: all in the directory, selected test directory (app/src/test) and app module.
  4. Run tests
-1
votes

In my case this was cause by an exception being thrown in the @BeforeClass method.

-3
votes

I had the same issue. I created Suite Class and it resolved the issue

-4
votes

Solved it using a lower gradle version

dependencies {
    classpath 'com.android.tools.build:gradle:2.0.0'

That will have to do for now