无法在“编辑配置”中选择“测试类”

时间:2016-01-16 15:59:43

标签: java android android-studio android-espresso android-uiautomator

我创建了Espresso / UIAutomator单元测试。但是,当我尝试运行它时,Android Studio无法识别它。选择TestLogin.java的按钮显示为灰色。我正在使用Android Studio 2.0预览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());

    }

我的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 Studio的图片。 Android Studio

Edit2:已启用所有测试工件(单元测​​试和检测测试)。 it's already enabled

2 个答案:

答案 0 :(得分:0)

再添加一个名为test的包:com.greenrobot.yesorno.test。然后将TestLogin测试放入其中。这应该可以解决你的问题。

另请注意:ActivityInstrumentationTestCase2已弃用,无法与ActivityTestRule一起使用。只保留ActivityTestRule并删除扩展部分和构造函数。我还建议使用setUp()注释添加@Before方法并在那里实例化mDevice,以便它可用于您在测试类中进行的每项测试。< / p>

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

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

答案 1 :(得分:0)

我聘请了Upwork的开发人员为我调查这个问题。主要问题是我的androidTest src指令缺少一个&#39; src&#39;

    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 Stackoverflow,你应该允许我选择文件,然后将它们显示为代码。