Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have created an Espresso/UIAutomator unit test. However, when I try to run it, Android Studio won't recognize it. The button to select TestLogin.java is greyed out. I'm using Android Studio 2.0 preview 5. Edit configurations screenshot

package com.greenrobot.yesorno.test.TestLogin

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

import org.junit.Rule;
import org.junit.Test;
import org.junit1.runner.RunWith;
import android.support.test.espresso.*;


/**
 * Created by andytriboletti on 1/15/16.
 */
@RunWith(AndroidJUnit4.class)
@LargeTest
public class TestLogin extends ActivityInstrumentationTestCase2<StartActivity> {
    public TestLogin(Class<StartActivity> activityClass) {
        super(activityClass);
    }
    private UiDevice mDevice;

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

    @Test
    public void testLogin() {
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

        // Start from the home screen
        mDevice.pressHome();
        onView(withText("Hello world!")).check(matches(isDisplayed()));

        onView(withId(R.id.changeTextBt)).perform(click());

    }

My build.gradle:

   buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'




android {
    aaptOptions.setProperty("cruncherEnabled", false)

    sourceSets {

        androidTest {
            java.srcDirs = ['androidTest/java']
        }
    }

    lintOptions {
        // set to true to turn off analysis progress reporting by lint
        quiet true
        // if true, stop the gradle build if errors are found
        abortOnError false
        // if true, only report errors
        ignoreWarnings true
    }

    productFlavors {
        // The actual application flavor
        production {
            minSdkVersion 15
        }
        // Test application flavor for uiautomatior tests
        myTest {
            minSdkVersion 18
        }
    }
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.greenrobot.yesorno"
        minSdkVersion 15
        targetSdkVersion 22
        multiDexEnabled = true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    packagingOptions {
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/maven/com.google.guava/guava/pom.properties'
        exclude 'META-INF/maven/com.google.guava/guava/pom.xml'
    }
}

repositories {
    mavenCentral()
    maven { url 'https://maven.fabric.io/public' }
}

repositories {
    maven { url "http://jzaccone.github.io/SlidingMenu-aar" }

}
repositories {
    maven { url "http://dl.bintray.com/populov/maven" }

}



dependencies {
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.robbypond:mopub-android-sdk:3.9.0'
    compile 'com.facebook.android:facebook-android-sdk:4.7.0'
    compile 'com.jeremyfeinstein.slidingmenu:library:1.3@aar'
    compile 'com.android.support:support-v4:23.1.1'
    compile files('libs/FlurryAgent.jar')
    compile files('libs/autobahn-android-0.5.2-SNAPSHOT.jar')
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    compile project(':viewpagerindicator')
    compile files('libs/gcm.jar')
    compile 'com.jakewharton.timber:timber:3.1.0'
    compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
        transitive = true;
    }
    compile 'com.squareup.okhttp3:okhttp:3.0.1'


    compile 'com.android.support:support-annotations:23.1.1'
    compile 'com.google.guava:guava:18.0'

    // Testing-only dependencies
    androidTestCompile 'com.android.support:support-annotations:23.1.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'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.0.0'
}

Edit: Here's a picture of my Android Studio. Android Studio

Edit2: Enable all test artifacts (Unit Testing and Instrumentation Test) is already enabled. it's already enabled

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
150 views
Welcome To Ask or Share your Answers For Others

1 Answer

Add one more package with name test like that: com.greenrobot.yesorno.test. Then put TestLogin test inside it. This should solve your problem.

ALSO: ActivityInstrumentationTestCase2 is deprecated and can't be used together with ActivityTestRule. Leave only ActivityTestRule and remove extends part and constructor. I'd also suggest to add setUp() method with @Before annotation and instantiate mDevice there, so it will be available for each test you'll have in test class.

UPDATE: also remove this, sync and clean:

 androidTest {
     java.srcDirs = ['androidTest/java']
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...