45

I'm using AndroidJUnitRunner with Espresso.

I wrote a simple test but always receive this exception. According to Stackoverflow answers, the problem is messing up the JUnit3 and JUnit4 but I have never used JUnit3 in my project.

junit.framework.AssertionFailedError: No tests found in com.walletsaver.app.test.espresso.SignUpPopupTest

package com.walletsaver.app.test.espresso;

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;

import com.walletsaver.app.activity.LoginActivity;

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.matcher.ViewMatchers.withText;

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

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

    @Test
    public void checkSignUpPopup() throws Exception {
        onView(withText("Sign Up")).perform(click());
    }
}

Run configuration: enter image description here

Output: enter image description here

Val
  • 4,225
  • 8
  • 36
  • 55

2 Answers2

126

I found the problem. It was missed code in build.gradle in the main module. If you have this problem I advise to start with adding this line:

android {
    ...

    defaultConfig {
        ...

        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
    }
...
}
Val
  • 4,225
  • 8
  • 36
  • 55
  • 1
    Thanks alot it solved the problem, can you tell why we have to add this? – Omer Malik Aug 29 '16 at 09:33
  • @OmerMalik I tried to mention `android.support.test.runner.AndroidJUnitRunner` in Run configuration but this didn't have an effect. Then I wrote it directly in `build.gradle` and it helped. It seems it should be set in `defaultConfig` to be visible. – Val Aug 31 '16 at 10:35
  • I read the android documentation and it is mentioned their, I missed the step, thank alot – Omer Malik Aug 31 '16 at 10:54
  • as a follow-up, one may see the message "Unable to find instrumentation info for". To fix that, tell the Android Studio launch configuration to use the specific test runner from the _build.gradle_ file – Del Sep 07 '16 at 19:54
  • 13
    For AndroidX, testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' – xyz Aug 07 '19 at 07:33
  • 1
    Hi, I have Dynamic Feature Modules in my app. For tests in the Feature Module, I'm using a custom testInstrumentationRunner , which is located in the app module. I'm getting this error "Test running failed: Instrumentation run failed due to 'Process crashed.'" Any idea how will it work across multiple modules? @Val – huskygrad Jul 24 '20 at 00:28
  • For AndroidX, use `testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"` instead. – Chenhe Sep 12 '20 at 04:00
5
android {
    defaultConfig {
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
    }
}

dependencies {
    androidTestImplementation "androidx.test:runner:1.4.0"
    androidTestImplementation "androidx.test.ext:junit:1.1.3"
}

@RunWith(AndroidJUnit4::class)
class SomeInstrumentedTest {}
Braian Coronel
  • 22,105
  • 4
  • 57
  • 62