当我想将它用于个人apk测试时,'android.support.test.espresso不存在'

时间:2015-01-15 03:44:41

标签: android android-testing android-espresso

我需要在没有源代码的情况下为Android应用程序执行一些自动测试工作。我发现机器人和意式浓缩咖啡都可以完成这项工作,我决定使用浓缩咖啡,因为它支持Google。

我想用相同的签名签署目标apk和espresso test apk,目标apk与this sample相同。

enter image description here

当我开始编写espresso测试apk时,我做了以下工作。

Module:app中的build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "tk.deedog.i01test"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'

    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
}

我的I01Test.java的源代码:

package tk.deedog.i01test;

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;

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;


public class I01Test extends ActivityInstrumentationTestCase2 {
    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "tk.tubabe.instrustmen101.Instruments101";
    private static Class<?> launcherActivityClass;
    Activity i101Acitvity;

    static {
        try {
            launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public I01Test() {
        super(launcherActivityClass);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        i101Acitvity = getActivity();
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testButtonClick() {
        onView(withText("Send")).perform(click());
    }
}

当我尝试运行此程序时,Android工作室告诉我Error:(6, 44) error: package android.support.test.espresso does not exist

6 个答案:

答案 0 :(得分:6)

我有同样的问题,通过检查我的项目结构让它工作。你的测试是在src / androidTest下吗?我不得不将我的文件夹名称从test重构为androidTest

答案 1 :(得分:4)

我有同样的问题,有一件事破坏了我的测试版本是以下几行:

main { java.srcDirs = ['src/main/java', 'src/AndroidTests', 'src/AndroidTest/java'] }

从srcDirs中删除测试代码:

sourceSets {
    ....
    main { java.srcDirs = ['src/main/java'] }
}

答案 2 :(得分:1)

太晚但可能会帮助别人。

我的情景就是这个 -

我正在运行Android Studio 2.2(稳定版)。我的androidTest文件夹在src /里面。在“Android”视图中,我的java文件夹显示了普通的java文件夹,测试文件夹,androidTest文件夹以及名为“java”(绿色)的androidTest文件夹的副本。 罪魁祸首是

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

将此更改为以下内容 -

sourceSets {
....
main { java.srcDirs = ['src/main/java'] }
...
}

我的应用级build.gradle看起来像这样

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.2"

defaultConfig {
    applicationId "my.package.name"
    minSdkVersion 16
    targetSdkVersion 24
    versionCode 1
    versionName "1.0-debug"
    multiDexEnabled true
    testInstrumentationRunner    "android.support.test.runner.AndroidJUnitRunner"

}

packagingOptions {
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE'

}
buildTypes {
    release {
        shrinkResources true
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
dexOptions {
    preDexLibraries = false;
}
sourceSets { main { java.srcDirs = ['src/main/java'] } }
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:design:24.2.0'
compile 'com.android.support:support-v13:24.2.0'
androidTestCompile 'junit:junit:4.12'

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') {
    // Necessary if your app targets Marshmallow (since Espresso
    // hasn't moved to Marshmallow yet)
    exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile('com.android.support.test:runner:0.5') {
    // Necessary if your app targets Marshmallow (since the test  runner
    // hasn't moved to Marshmallow yet)
    exclude group: 'com.android.support', module: 'support- annotations'
}


}
apply plugin: 'com.google.gms.google-services'

答案 3 :(得分:1)

在我的应用程序build.gradle上,缺少此依赖项:
androidTestImplementation 'com.android.support.test:rules:1.0.2'

答案 4 :(得分:0)

我没有清楚地明白你的意思

  

...没有源代码的Android应用程序。

但为什么你不能在没有反思的情况下从测试类调用活动?它是如何在 Espresso Start Guide 中显示的。

您有activity和适当的test,请查看这些示例中的包,导入的类,扩展类和构造函数。我的意思是这样的事情:

package com.google.android.apps.common.testing.ui.espresso.tests;

import static com.google.android.apps.common.testing.ui.espresso.Espresso.onData;
<...>

import com.google.android.apps.common.testing.ui.testapp.R;
import com.google.android.apps.common.testing.ui.testapp.SimpleActivity;

import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;

@LargeTest
public class BasicTest extends ActivityInstrumentationTestCase2<SimpleActivity> {

  public BasicTest() {
    super(SimpleActivity.class);
  }

  @Override
  public void setUp() throws Exception {
    super.setUp();
    getActivity();
  }

  <...>

答案 5 :(得分:0)

我认为最好不要定义自定义sourceSet。我只是评论

sourceSets

来自我的build.gradle及其正常工作。