'Error "No tests found" when running Android instrumentation tests

I am a beginner to testing. I have created a simple test case for login activity in android studio. But I got an error and I could not solve it. Here is my test code. Help will be really appreciated.

package com.example.hassidiczaddic.testinglist;

import android.app.Application;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import android.test.ApplicationTestCase;
import android.test.suitebuilder.annotation.LargeTest;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class ApplicationTest {

    public static final String STRING_TO_BE_TYPED = "Wolfmatrix";

    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);

    @Test
    public void LoginActivity() {
        onView(withId(R.id.etFName))
                .perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());
        onView(withId(R.id.etLName))
                .perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());
        onView(withId(R.id.btnSubmit))
            .perform(click());
        onView(withId(R.id.tvView))
            .check(matches(withText(STRING_TO_BE_TYPED)));
    }

}

This is my error:

Running tests

$ adb shell am instrument -w -r   -e debug false -e class       
com.example.hassidiczaddic.testinglist.ApplicationTest
com.example.hassidiczaddic.testinglist.
test/android.test.InstrumentationTestRunner
Client not ready yet..Test running started
junit.framework.AssertionFailedError: No tests found in     
com.example.hassidiczaddic.testinglist.ApplicationTest
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at    
android.test.InstrumentationTestRunner.onStart
(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run
(Instrumentation.java:1619)
Tests ran to completion.

Here is my gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.hassidiczaddic.testinglist"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
        }
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.0.1'
        // App dependencies
        compile 'com.android.support:support-annotations:23.0.1'
        compile 'com.google.guava:guava:18.0'
        androidTestCompile 'com.android.support:support-annotations:23.0.1'
        androidTestCompile 'com.android.support.test:runner:0.4.1'
        androidTestCompile 'com.android.support.test:rules:0.4.1'
        androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    }
}


Solution 1:[1]

If you are targeting SDK Version 28, you'll be using the re-packaged androidx classes from the support library, so AndroidJUnitRunner is configured like so...

android {
    defaultConfig {
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }
}

Solution 2:[2]

check build.gradle

replace testInstrumentationRunner "androidx.test.runner.AndroidJUnit4"

android {
    defaultConfig {
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }
}

Look for app/src/androidTest/AndroidManifest.xml

change android:name to androidx.multidex.MultiDexApplication

<application
        android:allowBackup="true"
        android:label="Components Tests App"
        android:supportsRtl="true"
        android:name="android.support.multidex.MultiDexApplication">

To

<application
            android:allowBackup="true"
            android:label="Components Tests App"
            android:supportsRtl="true"
            android:name="androidx.multidex.MultiDexApplication">

More suggestions: No tests found when running instrumented tests with AndroidX

Solution 3:[3]

If you landed here by generating the tests from the method... Try renaming the test method in your test class.. to something similar to actualMethod() to testActualMethod()

Solution 4:[4]

Check the logcat!

This can happen for a multitude of reasons and the build logs aren't always that helpful, even when using the --stracktrace --info, or --debug flags.

In my case, this was due to a missing runtime dependency. I only figured this out after checking the logcat and seeing the error there.

If you've checked all the obvious things mentioned in the other answers and your still having this issue, check the logcat for errors.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eurig Jones
Solution 2 xdeepakv
Solution 3 Priyanthi
Solution 4 SBerg413