22

I am new to Android instrumented unit tests. I have a simple test that looks like this. This is in instrumented tests; before I really write an actual instrumented test, I just want to test a simple one:

@RunWith(AndroidJUnit4.class)
@SmallTest
public class AddressBookKeepingInstrumentedTest {


    public static final String TEST_STRING = "This is a string";
    public static final long TEST_LONG = 12345678L;

    @Test
    public void test_simple() {
        assertEquals(2,1+1);
    }
}

When I run this, I get the following error:

junit.framework.AssertionFailedError: No tests found in com.example.myfirstapp.AddressBookKeepingInstrumentedTest
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1729)

Tests ran to completion.

Gradle build passed with success before this. Any ideas why this is happening?

Janusz
  • 187,060
  • 113
  • 301
  • 369
Serban Stoenescu
  • 3,136
  • 3
  • 22
  • 41

6 Answers6

34

Please add the following into your build.gradle and put your test classes into androidTest folder:

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

For AndroidX projects you should use:

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
user16217248
  • 3,119
  • 19
  • 19
  • 37
Long Ranger
  • 5,888
  • 8
  • 43
  • 72
  • The classes are in the right folder. I added what you said, now I get another error:Test running failed: Unable to find instrumentation info for: ComponentInfo{com.example.myfirstapp.test/android.support.test.runner.AndroidJUnitRunner} Empty test suite. What test suite is it talking about? I didn't create one. – Serban Stoenescu Sep 01 '16 at 08:09
  • Actually, the answer in this question helped further: http://stackoverflow.com/questions/39241640/android-instrumented-test-no-tests-found/39241996#39241996 – Serban Stoenescu Sep 01 '16 at 08:13
  • 1
    Can this be helpful ? http://stackoverflow.com/questions/29930597/android-studio-espresso-testing-error-empty-test-suite I did not have the same error as you when I created a simple hello world project with your class. I can run it successfully. – Long Ranger Sep 01 '16 at 08:20
  • Yes, that is actually what I did. I posted the wrong link in the comment. Thanks. Yes, it helped! – Serban Stoenescu Sep 01 '16 at 08:44
  • https://stackoverflow.com/questions/9476968/could-not-be-found-or-has-no-tests-in-junit/46556983#46556983 – Imeshke Oct 17 '17 at 05:24
  • See @whla's answer for AndroidX – E.M. Aug 04 '22 at 18:52
15

I recently tried the accepted answer, but it gave me a process crash error when trying to run. If you get this you should use:

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

Source: Instrumentation run failed due to 'Process crashed.'

whla
  • 739
  • 1
  • 12
  • 26
3

The typical cause of this can be seen as an exception in Logcat.

I had a specific issue regarding an instrumented multidex run on < API 21, I had to both install MultiDex on a subclass of androidx.test.runner.AndroidJUnitRunner, and override:

  • AndroidJUnitRunner::createTestRequestBuilder
  • TestRequestBuilder::createClassPathScanner
  • ClassPathScanner::getClassPathEntries with the implementation as:

    • Unzip and extract the dex files from the APK
    • Run dexlib2 to obtain the type names.
    • Convert from the type names to a readable format: Ljava/lang/String; -> java.lang.string

This is because ClassPathScanner uses dalvik.system.DexFile, which on API 16, only reads APKs, and then only reads classes.dex

DA_123
  • 337
  • 3
  • 10
2

Another reason to get this error message is when you have an Android App implemented with Java and test cases implemented with Kotlin. If that is the case, be sure you have the following lines in your gradle file:

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
RobertoAllende
  • 8,744
  • 4
  • 30
  • 49
1

This post helped me, just rename your tests methods with "test...", like testMethod()

Community
  • 1
  • 1
Pablo Delgado
  • 41
  • 1
  • 9
0

Make sure you added the correct dependencies in the app's build.gradle file:

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

The Expresso one is required for any instrumented test even if you don't use it.

Mister Smith
  • 27,417
  • 21
  • 110
  • 193