Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No tests found in <package> when testing with Espresso

I want to test my MainActivity in my Android application. So, I created a first test case which tests the functionality of a button. If the user clicks this particular button, a new Activity should open.

Here is my code:

@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

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

    @Test
    public void startNewActivityTest() {
        Intents.init();
        onView(withId(R.id.main_new)).perform(click());
        intended(hasComponent(NewActivity.class.getName()));
        Intents.release();
    }

    @Before
    public void setup() {
        closeSoftKeyboard();
    }

}

Unfortunately, I get the following exception:

junit.framework.AssertionFailedError: No tests found in package.MainActivityTest
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:1701)

Here is my Gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion "23.0.1"

    lintOptions {
        abortOnError false
    }

    defaultConfig {
        applicationId "package"
        minSdkVersion 14
        targetSdkVersion 19
    }

    signingConfigs {
        debug {
            storeFile file(RELEASE_STORE_FILE)
            storePassword RELEASE_STORE_PASSWORD
            keyAlias RELEASE_KEY_ALIAS
            keyPassword RELEASE_KEY_PASSWORD
        }
        release {
            storeFile file(RELEASE_STORE_FILE)
            storePassword RELEASE_STORE_PASSWORD
            keyAlias RELEASE_KEY_ALIAS
            keyPassword RELEASE_KEY_PASSWORD
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

dependencies {
    compile 'com.android.support:support-v4:19.1.0'
    compile 'com.google.android.gms:play-services:4.2.+'
    androidTestCompile 'com.android.support:support-annotations:19.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'
    // Set this dependency if you want to use Hamcrest matching
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
}
like image 366
user3475602 Avatar asked Dec 19 '15 07:12

user3475602


People also ask

Is Espresso black box testing?

Espresso is targeted at developers, who believe that automated testing is an integral part of the development lifecycle. While it can be used for black-box testing, Espresso's full power is unlocked by those who are familiar with the codebase under test.

How do you check espresso visibility?

One simple way to check for a View or its subclass like a Button is to use method getVisibility from View class. I must caution that visibility attribute is not clearly defined in the GUI world. A view may be considered visible but may be overlapped with another view, for one example, making it hidden.


1 Answers

Set the instrumentation runner

Add to the same build.gradle file the following line in android.defaultConfig: testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

apply plugin: 'com.android.application'

android {
    ...

    defaultConfig {
        ...

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

dependencies {
    // App's dependencies, including test
    compile 'com.android.support:support-annotations:23.0.1'

    ...
}

I never used Espresso Intents but perhaps you need this like here:

Use IntentsTestRule instead of ActivityTestRule when using Espresso-Intents. IntentsTestRule makes it easy to use Espresso-Intents APIs in functional UI tests. This class is an extension of ActivityTestRule, which initializes Espresso-Intents before each test annotated with @Test and releases Espresso-Intents after each test run. The activity will be terminated after each test and this rule can be used in the same way as ActivityTestRule.

like image 172
albodelu Avatar answered Oct 17 '22 00:10

albodelu