'android.support.test.espresso does not exist' when I want to use it to individual apk test

前端 未结 6 1289
-上瘾入骨i
-上瘾入骨i 2021-02-03 12:09

I need to do some auto testing jobs to an Android application without its source code. I found both robotium and espresso can do this job, I decided to use espresso because its

相关标签:
6条回答
  • 2021-02-03 12:18

    I had the same issue and got it to work by checking my project structure. Are your tests under src/androidTest ? I had to refactor the name of my folder from test to androidTest

    0 讨论(0)
  • 2021-02-03 12:18

    I had this same issue, one thing that was ruining my test build is the following line:

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

    Remove test code from srcDirs:

    sourceSets {
        ....
        main { java.srcDirs = ['src/main/java'] }
    }
    
    0 讨论(0)
  • 2021-02-03 12:27

    I think you'd be better off not defining custom sourceSets. I just comment

    sourceSets

    from my build.gradle and its working fine.

    0 讨论(0)
  • 2021-02-03 12:36

    Too late but may help someone.

    My scenario was this -

    I was running Android Studio 2.2(Stable). My androidTest folder was inside src/ . In the "Android" view, my java folder was showing normal java folder,test folder ,androidTest folder and a copy of the the androidTest folder under the name "java" (in green). The culprit was

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

    Change this to the following -

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

    My app level build.gradle looks like this

    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'
    
    0 讨论(0)
  • 2021-02-03 12:36

    I didn't clearly understand what did you mean under

    ... Android application without its source code.

    but why can't you call activity from the test class without reflection? Just how it was shown in Espresso Start Guide.

    You have activity and appropriate test, take a look at the package, imported classes, extended class and constructor in those examples. I mean smth like this:

    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();
      }
    
      <...>
    
    0 讨论(0)
  • 2021-02-03 12:39

    On my app build.gradle this dependency was missing:
    androidTestImplementation 'com.android.support.test:rules:1.0.2'

    0 讨论(0)
提交回复
热议问题