Why is the Android test runner reporting "Empty test suite"?

AndroidIntellij Idea

Android Problem Overview


I am banging my head against the wall here trying to figure out why IntelliJ/Android is reporting "Empty test suite". I have a small project with two IntelliJ Modules ("Projects" in Eclipse). The Unit test module has its own AndroidManifest.xml, which I have pasted at the bottom. I am trying to run an ActivityUnitTestCase, since the tests will be dependent upon the Context-object.

The package name of the main module is nilzor.myapp. The pacakge name of the test module is nilzor.myapp.tests

Why is not the test runner detecting the testBlah()-method as a test?

<?xml version="1.0" encoding="utf-8"?>
<!-- package name must be unique so suffix with "tests" so package loader doesn't ignore us -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
		  package="nilzor.myapp.tests"
		  android:versionCode="1"
		  android:versionName="1.0">
	<!-- We add an application tag here just so that we can indicate that
		 this package needs to link against the android.test library,
		 which is needed when building test cases. -->
	<application>
		<uses-library android:name="android.test.runner"/>
	</application>
	<!--
	This declares that this application uses the instrumentation test runner targeting
	the package of nilzor.myapp.  To run the tests use the command:
	"adb shell am instrument -w nilzor.myapp.tests/android.test.InstrumentationTestRunner"
	-->
	<instrumentation android:name="android.test.InstrumentationTestRunner"
					 android:targetPackage="nilzor.myapp"
					 android:label="Tests for nilzor.myapp"/>
</manifest>

And here is my test class:;

package nilzor.myapp.tests;

public class NilzorSomeTest<T extends Activity> extends ActivityUnitTestCase<T>{
	public NilzorSomeTest(Class<T> activityClass){
	    super(activityClass);
	}

	@SmallTest
	public void testBlah(){
	    assertEquals(1,1);
	}
}

I have read the testing fundamentals, the activity testing document, and tried following this Hello world test blog, even though it is for Eclipse. I cannot get the test runner to find and run my test. What am I doing wrong?

Some of the questions I still feel unsure about are:

  1. Do I need an Annotation above the Unit test method?
  2. Do I need to prefix the method with "test", or is that just for JUnit tests?
  3. Can I have tests in sub-packages of nilzor.myapp.tests?

But the main question of this post is why does not the test runner detect my test?

Android Solutions


Solution 1 - Android

You need to provide default constructor for your test class, for example:

package nilzor.myapp.tests;

public class NilzorSomeTest extends ActivityUnitTestCase<ActivityYouWantToTest>{
    public NilzorSomeTest(){
        super(ActivityYouWantToTest.class);
    }

    @SmallTest
    public void testBlah(){
        assertEquals(1,1);
    }
}

about your other questions:

  1. No. My tests still run without any annotations, but I guess it's a good practice to have them. It allows you to specify size of tests to run. See https://stackoverflow.com/questions/4671923/what-is-the-purpose-of-smalltest-mediumtest-and-largetest-annotations-in-an for more detail.

  2. Yes, you need "test" prefix. InteliJ gives "method never used" warning when there's no "test" prefix, and skips that method during test run.

  3. Yes. I have my tests organized into subpackages and it seems to be working well.

Solution 2 - Android

If this is happening "all of a sudden" or "it was working 5 minutes ago" my solution was to go into Run/Debug configurations and remove any configurations under "Android Tests". Sometimes these configurations get corrupted if I refactor the class under test (for example by moving to an new package).

enter image description here

Solution 3 - Android

None of the above fixed it for me. What helped was following the instructions:

>

Create a test configuration

> >

In Android Studio:

> >
  • Open Run menu -> Edit > Configurations
  • Add a new Android Tests > configuration
  • Choose a module
  • Add a specific > instrumentation runner:
> >
 data-lang="groovy">  android class="o">.support class="o">.test class="o">.runner class="o">.AndroidJUnitRunner
>
> >

Run the newly created configuration.

Solution 4 - Android

I had a similar issue. Not sure why this is occurring but I was able to fix it by going to: "File" > "Invalidate Caches/Restart" in Android Studio.

Solution 5 - Android

I don't know if it helps for Android Studio, but I had some kind of Intellij-Gradle conflict. Solved it by "right-clicking" on the test-file and hit "compile file ...Test.java". After that I could run single tests again.

Solution 6 - Android

I had the same issue on Android Studio 2.3.1, turns out it was just a bug with AS. Running the same test on version 2.2.1 performs fine.

If you're only running Android Studio on the Cannary channel, I recommend you also install a stable version as well. http://tools.android.com/tips/using-multiple-android-studio-versions

Solution 7 - Android

I had tests that were running fine until gradle and android studio got upgraded.

Apart from adding a default constructor to your tests, you might need to do some of these things to get your test suite to work

Under src/ create androidTest/java/<your-package-name>/test . Note the androidTest. Anything else including instrumentTest will not work.

Add this to build.gradle

sourceSets {
    testLocal {
        java.srcDir file('src/androidTest/java')
        resources.srcDir file('src/androidTest/resources')
    }
}



android{
    sourceSets {
       instrumentTest.setRoot('src/androidTest/')
    }
}

dependencies{
     testLocalCompile 'junit:junit:4.11'
}

task localTest(type: Test, dependsOn: assemble) {
    testClassesDir = sourceSets.testLocal.output.classesDir

    android.sourceSets.main.java.srcDirs.each { dir ->
        def buildDir = dir.getAbsolutePath().split('/')
        buildDir = (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')

        sourceSets.testLocal.compileClasspath += files(buildDir)
        sourceSets.testLocal.runtimeClasspath += files(buildDir)
    }

    classpath = sourceSets.testLocal.runtimeClasspath
}

check.dependsOn localTest

Add this to the AndroidManifest.xml

 <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:label="Tests for my packaged app"
        android:targetPackage="<my-package-name>.test" />

Solution 8 - Android

For Intellij 15 I resolved this issue by:

  1. Opening the 'Project Structure' settings
  2. Clicking 'Modules' (on left)
  3. 'Sources' Tab
    a. Right click on your source directory (usually src) click 'Source'.
    b. Right click on your test directory click 'Test'
    c. Right click on your out directory click 'Excluded'
  4. Go to 'Paths' tab
    a. Click 'Use module compile output path' radio button
    b. Select your output path directory for 'Output Path'
    c. Select your test path directory for 'Test output Path'
  5. Click Ok

Solution 9 - Android

Obviously, you need a target device as to run your tests as they are instrumented tests. For some reasons, Android studio sometimes does not ask you to point to this target device and just prompt the "Empty Test Suite" message. There are different ways to fix this, here are a few :

  • run your main app and select a target device or

  • go to the Run (Run/Run.../Edit Configurations) configuration and modify the Deployement Target Options

Solution 10 - Android

I had this problem because I had this in my build.gradle:

testOptions {
    execution "ANDROID_TEST_ORCHESTRATOR"
}

Even though I wasn't using the Android Test Orchestrator (must have copyied from the tutorials by mistake).

Commenting that out solved it for me.

Solution 11 - Android

In my case, none of the previous answers worked. The solution was to simply move the test class to another package.

This happened under androidTest/

Solution 12 - Android

In my case that problem was caused due to mistake in my code, actually that was in application class, so target activity wasn't opened and test output prints

> Empty test suite error

I have tried run tests directly from terminal with adb shell am instrument -w -r -e package your.package -e debug false android.support.test.runner.AndroidJUnitRunner. With this it prints for you much more about exception.

Solution 13 - Android

None of the other solutions worked for me, but I was able to get this working simply by uninstalling the existing app or test suite, then running the tests.

Solution 14 - Android

In my case, the project I was working on had a couple of modules. None of the solutions I found for this error helped me, and then somehow I realized that if I added the testing dependencies in BOTH of the build.gradle files, the tests magically started working. It doesn't matter if your tests live in only 1 of the modules, both gradle files must include the dependencies and the testInstrumentationRunner value.

So, if like me, none of the other answers have helped you, try adding these lines to the build.gradle file of each of your modules:

android {    
    ....
    defaultConfig {
        ...
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }  
}

and then also add:

dependencies {
    ...
    // Test
    androidTestCompile 'com.android.support:support-annotations:23.4.0'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test:rules:0.5'

}

Solution 15 - Android

I just renamed the file and the problem fixed.

Solution 16 - Android

I had the same issue, and the reason was my test class did not have Test at the end of the class name!

Solution 17 - Android

My issue was caused by an exception being thrown in the @BeforeClass method of my test case. It some how wasn't causing the test to fail - I only found it by inspecting the logcat output.

I fixed the exception and suddenly my tests were running!

Solution 18 - Android

After facing the problem today - not being able to run the instrumented android tests with Empty suite error - I found a git issue about this problem and thanks to Stephan Linzner, I could run the tests.

tl;dr You have to right click the test package and not the class in order to make the tests run.

Reference: https://github.com/googlecodelabs/android-testing/issues/27#issuecomment-219074863

Solution 19 - Android

This article helped me: Empty test suite

Basically I had to create a package - instrumentTest/java - under my src directory, and put all the tests there. Then I could execute these tests individually.

Solution 20 - Android

I had a raw Java project where this was occurring. Simply Java + JUnit4. It definitely resides with something in your .idea/ or .iml files. I scrapped mine, re-imported, and finally the tests ran again.

Solution 21 - Android

The test class may excluded from the compilation. Fix it in setting-compiler-exclude.

Solution 22 - Android

Here are my debugging steps I go through when Android Studio all of a sudden decides to stop running / debugging tests (And boy does this happen embarassingly often!!):

  • Build: → Rebuild project
  • Restart Device: Restart your device/emulator and try again
  • Device switch: if you have both a regular phone and an emulator unplug one and try running it with just one of the devices
  • Android Studio: File--> Invalidate caches and restart
  • Activity Monitor / Task Manager: sort processes by name, see if there is a nameless processes that's using up a lot of ram, this is a "ghost" process from Android studio that must be killed
  • git revert: try stashing /reverting your latest code. Sometimes there is a compile error that Android Studio / gradle misses and it will just try to run uncompilable code.
  • Uninstall then reinstall Android Studio.

I will add more fixes as I run into them!

Solution 23 - Android

I did nothing and the problem went away after half a day of pain, I opened and closed the projects many times, ran each class tests manually, maybe that fixed my it.

Solution 24 - Android

In my case, I had my instrumented tests in androidTest/java/<package.name>/MyTestingClass, but I had set my current build variant to "preproduction". And there's the point! As specified in Android Studio documentation:

> By default, all tests run against the debug build type.

The message Class not found. Empty test suite. kept appearing until I did this:

  1. Add this line to my build.gradle:

    android{
        [...]
        testBuildType "preproduction"
    }
    
  2. Synchronised gradle.

  3. Delete my previous test configurations since they don't take this Gradle synchronisation into account.

Then I executed the tests again and this time they run just perfect!!!

Solution 25 - Android

In Android studio with spock framework I've changed my gradle's version from 2.2.2 to 3.2.1 and all goes well.

Solution 26 - Android

The accepted answer didn't solve my problem. So I decided to copy ExampleInstrumentedTest which is created by default in Android Studio and runs without any problems, renamed it during the copy process (no Refactor->Rename after copying!) and pasted the contents of my unit test into it. After that the error disappeared.

Solution 27 - Android

I experienced the "Empty test suite" error when trying to run local unit tests in my Android Studio 3.0 project.

After reading the Android Developer documentation, I quickly realised that the issue was caused by my gradle config which included the following lines.

testImplementation 'com.android.support.test:runner:0.5'
testImplementation 'com.android.support.test:rules:0.5'

The AndroidJUnitRunner class is a JUnit test runner that lets you run JUnit 3- or JUnit 4-style test classes on Android devices.

Since my tests were local and therefore not required to run on any device, removing the above com.android.support.test... entries enabled me to execute the unit tests.

Solution 28 - Android

I was doing some insertions in a db in the @BeforeClass method. I realised I had an object/database mapping problem. This data mapping problem was the cause of this issue for me.

Solution 29 - Android

I had this happen to me when I mistakenly marked a non mock class variable with the annotation @Mock Removed the annotation and the tests ran successfully. This happened with Junit 4.5 on Android Studio

Solution 30 - Android

Not a solution but a workaround that will get you back on track quickly:

  1. Firstly, find a test that works. I was writing a new test where I got the 'empty test suite' error. I ran other tests and they were working as usual.

  2. Copy the test file that does work. Run it to make sure this copy works like the original.

  3. Remove the body and replace it with your new test code.

The test should now work.

We spent about two hours trying to find the cause but to no avail.

Solution 31 - Android

I had the following block of code

@get:Rule
lateinit var activityScenario: ActivityScenario<HomeActivity>

because i needed to pass different parameters in the intent of activity based on the different test cases. When i removed the @get:Rule the tests started to work again.

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
QuestionNilzorView Question on Stackoverflow
Solution 1 - AndroidlmacView Answer on Stackoverflow
Solution 2 - Androidtir38View Answer on Stackoverflow
Solution 3 - Androidserv-incView Answer on Stackoverflow
Solution 4 - AndroidJeff StapletonView Answer on Stackoverflow
Solution 5 - AndroidkotlinskiView Answer on Stackoverflow
Solution 6 - AndroidAndrea ThackerView Answer on Stackoverflow
Solution 7 - AndroidPratik MandrekarView Answer on Stackoverflow
Solution 8 - AndroidMarquis BlountView Answer on Stackoverflow
Solution 9 - AndroidSylvainView Answer on Stackoverflow
Solution 10 - AndroidPabloView Answer on Stackoverflow
Solution 11 - AndroidMateus GondimView Answer on Stackoverflow
Solution 12 - AndroidBelooView Answer on Stackoverflow
Solution 13 - AndroidPhilView Answer on Stackoverflow
Solution 14 - AndroidJorge SalasView Answer on Stackoverflow
Solution 15 - AndroidLynView Answer on Stackoverflow
Solution 16 - AndroidJaninView Answer on Stackoverflow
Solution 17 - AndroidshockingView Answer on Stackoverflow
Solution 18 - AndroidFrancisco JuniorView Answer on Stackoverflow
Solution 19 - AndroidIgorGanapolskyView Answer on Stackoverflow
Solution 20 - AndroidcdaringeView Answer on Stackoverflow
Solution 21 - AndroidpetertcView Answer on Stackoverflow
Solution 22 - AndroidHRVHackersView Answer on Stackoverflow
Solution 23 - AndroidGary DaviesView Answer on Stackoverflow
Solution 24 - AndroidJuan José Melero GómezView Answer on Stackoverflow
Solution 25 - AndroidPsijicView Answer on Stackoverflow
Solution 26 - Androidka3akView Answer on Stackoverflow
Solution 27 - AndroidWill PView Answer on Stackoverflow
Solution 28 - AndroidLaurentView Answer on Stackoverflow
Solution 29 - AndroidMihirView Answer on Stackoverflow
Solution 30 - AndroidxavierdominguezView Answer on Stackoverflow
Solution 31 - AndroidVaiosView Answer on Stackoverflow