Serban Stoenescu
Serban Stoenescu

Reputation: 3896

Android instrumented test no tests found

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?

Upvotes: 24

Views: 20133

Answers (6)

Long Ranger
Long Ranger

Reputation: 6008

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"

Upvotes: 35

Mister Smith
Mister Smith

Reputation: 28199

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.

Upvotes: 0

RobertoAllende
RobertoAllende

Reputation: 9548

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'

Upvotes: 2

DA_123
DA_123

Reputation: 385

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

Upvotes: 3

whla
whla

Reputation: 811

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.'

Upvotes: 15

Pablo Delgado
Pablo Delgado

Reputation: 51

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

Upvotes: 1

Related Questions