36

I have created an example test case that extends AndroidTestCase. When I run the test case, it errors out by saying

Running tests
Test running startedTest running failed: 
Instrumentation run failed due to 'java.lang.RuntimeException'
Empty test suite.

The test case

import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

import static org.junit.Assert.assertEquals;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.lang.Exception;
import java.lang.Override;

public class DateFormatTest extends AndroidTestCase{

    @Override
    protected void setUp() throws Exception {
        super.setUp();
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public DateFormatTest(){
        super(DateFormatTest.class);
    }


    @SmallTest
    public void testMultiply() {

        assertEquals("10 x 5 must be 50", 50, 10*5);
    }
}
Pratik Mandrekar
  • 9,362
  • 4
  • 45
  • 65
  • I had a similar problem in a project with 2 modules and none of the answers I found helped me. I posted my solution to this on another questions. Hopefully it helps someone else: http://stackoverflow.com/questions/14381694/why-is-the-android-test-runner-reporting-empty-test-suite/38734699#38734699 – Jorge Salas Aug 03 '16 at 04:59

11 Answers11

17

Since nobody else mentions it: methods in AndroidTestCase subclasses need to be public and have names starting with "test"!

The OP and the answers all got this right but I missed it and got the exact same error.

Toerndev
  • 715
  • 1
  • 6
  • 21
12

I got this error when running tests from Android Studio. Turned out I had placed my test cases in the wrong folder. When running Gradle/Android Studio, all Android tests should be in the folder src/instrumentTest/java.

Edit: In Gradle plugin version 0.9 or later the correct name of the folder is androidTest. (http://tools.android.com/tech-docs/new-build-system/migrating_to_09)

emidander
  • 2,383
  • 22
  • 29
  • Where is it specified that this is the mandatory file structure for Android tests? – IgorGanapolsky Apr 09 '14 at 16:55
  • 1
    It's not actually mandatory, it's just the default folder structure. You can read more about it on the Android tools site: http://tools.android.com/tech-docs/new-build-system. When I wrote the answer above I could not get tests to work outside the default structure, but the Gradle Android build system is a work in progress so the problem might be gone by now. – emidander Apr 10 '14 at 07:47
  • 1
    per emidander's link above, a breaking change in the build system was put in so the new folder structure should be: src/androidTest/java – bsautner Apr 28 '14 at 17:04
  • 1
    this - anyone running into this issue today, its likely because you upgraded gradle and still have your folder named instrumentTest and not androidTest – stevebot Sep 23 '14 at 20:05
11

I understand that this question is old, and the development tools have changed significantly since this question has been asked.

However, I had a similar issue (in AndroidStudio 2.1.1) and it turned out that it was just the error message that was quite misleading. For me it said:

Test running started
Test running failed: Instrumentation run failed due to 'java.lang.IllegalStateException'
Empty test suite.

(Note: The difference to the original question is in IllegalStateException vs. RuntimeException)

It turned out that this IllegalStateException was actually thrown in the initialization of the application context as can be seen by inspecting the logcat output. As a result, no test-methods were run, and Android Studio writes this somewhat misleading "Empty test suite" error.

I.e. "Empty test suite" can mean what it actually says (you don't have any test methods declared, e.g. because you forgot to annotate them with @Test), but it can also mean that something (like a runtime exception thrown in your application initialization code) prevents the test-runner from reaching any test methods (which seems to be your case).

Check adb-logcat and search for RuntimeExceptions. This will probably find you the root-cause of your problem.

kuhnmi
  • 364
  • 3
  • 8
7

I got this error because one of my test methods didn't include "throws exception" after the method signature. might help somebody

dabluck
  • 1,641
  • 2
  • 13
  • 20
  • This was my source of error. I was actually throwing a few specific exceptions, just not the generic "Exception". Why this would be a requirement is beyond me since I didn't see it in the docs. – IcedDante May 25 '17 at 20:51
6

I got the same issue. I was having testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" in my defaultConfig {...}. I have just removed that line, and now it's working fine (The IDE is picking the right runner config on build time).

I hope this will help someone.

ahmed_khan_89
  • 2,755
  • 26
  • 49
5

My problem was I had default constructor generated by android studio, looked like this

public class SomeTest extends ActivityUnitTestCase<ActivityYouWantToTest>{
    public SomeTest(Class<NewsActivity> activityClass) {
        super(activityClass);
    }
}

and I had to change it to this to get rid of the problem

public class SomeTest extends ActivityUnitTestCase<ActivityYouWantToTest>{
        public SomeTest() {
            super(ActivityYouWantToTest.class);
        }
    }
Lukas Hanacek
  • 1,364
  • 14
  • 25
3

We use the AndroidTestCase class to define that we are testing components which are specific to Android. The main benefit of AndroidTestCase is that it gives us access to the application's Resources such as strings and layouts, etc.

The AndroidTestCase does not require you to overwrite the default constructor as it does not provide a particular Activity as the Context, but rather provides you a general one so you can still call getContext().

In your example, you are not using any Android components, so for you, the following would make sense:

import android.test.suitebuilder.annotation.SmallTest;

import junit.framework.TestCase;

public class DateFormatTest2 extends TestCase {

    @SmallTest
    public void testMultiply() {

        assertEquals("10 x 5 must be 50", 50, 10 * 5);
    }
}

Notice the use of TestCase rather than AndroidTestCase.

For AndroidTestCase to be applicable, a test that requires resources would be necessary:

import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

public class DateFormatTest extends AndroidTestCase {

    @SmallTest
    public void testAppTitle() {
        assertEquals("MyApp", getContext().getResources().getString(R.string.app_name));
    }
}

Here we use the AndroidTestCase because we need to access the application's resources.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
MariusVolkhart
  • 449
  • 1
  • 4
  • 12
1

This guide might help - http://www.slideshare.net/tobiaspreuss/how-to-setup-unit-testing-in-android-studio

On the latest gradle (0.9+) the test should be under androidTest dir

Also in your gradle.build:

dependencies {
     androidTestCompile 'junit:junit:4.+'
}

also add those under defaultConfig {

testPackageName "test.java.foo"
testInstrumentationRunner "android.test.InstrumentationTestRunner"

}

Gal Bracha
  • 19,004
  • 11
  • 72
  • 86
1

Did you configure your testRunner in your gradleConfig? We use different TestRunners for different tests (to speed things up. My config looks like this

android {    
// Some stuff      
  defaultConfig {
        // Some other stuff

        //junit test
        testInstrumentationRunner "de.qabel.qabelbox.QblJUnitRunner"
        //ui tests
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"   
    }  
}

If i disable one of this lines the corresponding test will also report "Empty Test Suite".

r-hold
  • 941
  • 8
  • 30
0

I just started learning about testing Android applications and I've been struggling with the same problem. 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);
    }
}
nitesh goel
  • 6,338
  • 2
  • 29
  • 38
0

I already have a default constructor in my test case but still it was giving me error "Empty test suite" and was stuck at "Instantiating tests...".

Tried creating new workspace, resetting Android Studio, but that didn't work.

Finally, close Android SDK and emulator.

  1. Go to your android-sdks/platform-tools.

  2. Clear all Android temp files with these commands:

    a. rm -rf ~/Library/Application Support/AndroidStudio

    b. rm -rf ~/Library/Caches/AndroidStudio

    c. rm -rf ~/Library/Application Support/AndroidStudio

    d. rm -rf ~/Library/Preferences/AndroidStudio

  3. Run:

    ./adb kill-server
    ./adb start-server
    
  4. Start Android and run test case.

Pang
  • 9,564
  • 146
  • 81
  • 122
StackTrace
  • 221
  • 5
  • 12
  • I think I tried everything mentioned in the answers to this question. Still I get the output: "Running tests Test running startedFinish Empty test suite." with no error. I asked a question at http://stackoverflow.com/questions/35426990/no-error-still-android-studio-says-no-tests-were-found – Monica Feb 16 '16 at 20:37