无法在编辑配置中 select 测试 class

Can't select Test class in Edit Configuration

我创建了一个 Espresso/UIAutomator 单元测试。但是,当我尝试 运行 它时,Android Studio 无法识别它。 select TestLogin.java 的按钮是灰色的。我正在使用 Android Studio 2.0 预览版 5。

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());

    }

我的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'
}

编辑:这是我的 Android 工作室的照片。

Edit2:启用所有测试工件(单元测​​试和仪器测试)已启用。

再添加一个名称为 test 的软件包:com.greenrobot.yesorno.test。然后把TestLogin测试放在里面。这应该可以解决您的问题。

另外:ActivityInstrumentationTestCase2 已弃用,不能与 ActivityTestRule 一起使用。仅保留 ActivityTestRule 并删除扩展部分和构造函数。我还建议添加带有 @Before 注释的 setUp() 方法并在那里实例化 mDevice,这样它就可以用于测试 class 中的每个测试。

更新:同时删除它,同步并清理:

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

我在 Upwork 上聘请了一名开发人员来帮我调查这个问题。主要问题是我的 androidTest src 指令缺少 'src'

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

我的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 = ['src/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" }
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}

def autobahn_version = '0.5.2-SNAPSHOT'

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 'de.tavendo:autobahn-android:' + autobahn_version
    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'
    androidTestCompile 'junit:junit:4.12'
}

我的TestLogin.java

    package com.greenrobot.yesorno.test;

import android.content.Context;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.Until;
import android.test.suitebuilder.annotation.LargeTest;

import com.greenrobot.yesorno.Home;

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.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;

import com.greenrobot.yesorno.R;


/**
 * Created by andytriboletti on 1/15/16.
 */
@RunWith(AndroidJUnit4.class)
@LargeTest
public class TestLogin  {

    private UiDevice mDevice;

    private static final String PACKAGE_NAME = "com.greenrobot.yesorno";
    private static final int LAUNCH_TIMEOUT = 5000;

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

    public TestLogin() {
        super();
    }

    @Before
    public void initTest() {
        // Initialize UiDevice instance
    
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

        // Start from the home screen
        mDevice.pressHome();

        // Wait for launcher
    
        String launcherPackage = mDevice.getCurrentPackageName();
        mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);

        // Launch the app
    
        Context context = InstrumentationRegistry.getContext();
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(PACKAGE_NAME);

        // Clear out any previous instances
    
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);

        // Wait for the app to appear
    
        mDevice.wait(Until.hasObject(By.pkg(PACKAGE_NAME).depth(0)), LAUNCH_TIMEOUT);
    }

    @Test
    public void testLogin() {
        onView(withId(R.id.welcome)).check(matches(isDisplayed()));

//        onView(withText("Hello world!")).check(matches(isDisplayed()));

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

    }
}

我花了 10 分钟试图让 TestLogin 格式化为代码,但它不起作用,所以我创建了一个 pastebin。 http://pastebin.com/9W3pT4rH Whosebug,你应该允许我 select 个文件,然后将它们显示为代码。