【问题标题】:What are the correct dependencies for Espresso tests?Espresso 测试的正确依赖项是什么?
【发布时间】:2020-04-23 16:35:54
【问题描述】:

在不同的官方 Android 网站上,关于如何设置 Espresso 测试的信息有所不同:

1) https://developer.android.com/training/testing/ui-testing/espresso-testing

dependencies {
    // Other dependencies ...
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

2) https://developer.android.com/studio/test/

dependencies {
    // Required for local unit tests (JUnit 4 framework)
    testCompile 'junit:junit:4.12'

    // Required for instrumented tests
    androidTestCompile 'com.android.support:support-annotations:24.0.0'
    androidTestCompile 'com.android.support.test:runner:0.5'
}

3)https://developer.android.com/training/testing/espresso/setup

androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
  • 在某些配置下,我会遇到库冲突((2) 说明这是由于依赖项需要相同的其他依赖项但版本不同)。

    • 即使用
    • 排除

    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2', { exclude group: 'com.android.support', module: 'support-annotations' } 可能没有帮助。

  • 使用某些配置,我在导入 android.support.test.rule.ActivityTestRule 时遇到错误

【问题讨论】:

    标签: dependencies android-espresso


    【解决方案1】:

    没有。 3 似乎工作,有和没有排除部分:

    dependencies {
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        }
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test:rules:1.0.2'
    }
    

    对于较旧的 espresso 版本,例如 2.2.2,似乎不需要为 android.support.test.rule.ActivityTestRule 导入单独的依赖项。

    【讨论】:

      【解决方案2】:

      Espresso 是一种仪器测试。这些与单元测试放在不同的文件夹中。它们保存在 androidTest/java 包中。因此,在使用依赖项时请记住这一点。对于仪器测试,您需要使用

      androidTestImplementation
      

      testImplementation
      

      所以假设您的 Espresso 测试类位于 app->src->androidTest->java 位置,这应该可以工作:

      androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
      androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
      androidTestImplementation 'com.android.support.test:rules:1.0.2'
      androidTestImplementation 'com.android.support.test:runner:1.0.2'
      

      【讨论】:

        【解决方案3】:

        对于可能会寻找这个的人,这是我的 build.gradle 设置与 Cucumber + Espresso + Barista:

        plugins {
            id 'com.android.application'
            id 'kotlin-android'
            id 'kotlin-android-extensions'
        }
        
        android {
            compileSdkVersion 29
        
            defaultConfig {
                applicationId "com.lomza.uitestsapp"
                minSdkVersion 23
                targetSdkVersion 29
                versionCode 1
                versionName "1.0"
        
                testApplicationId "com.lomza.uitestsapp.tests"
                testInstrumentationRunner "com.lomza.uitestsapp.tests.GroceriesAppAndroidTestRunner"
            }
        
            sourceSets {
                androidTest {
                    assets {
                        assets.srcDirs = ['src/androidTest/assets']
                    }
                }
            }
        
            buildTypes {
                release {
                    minifyEnabled false
                    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
                }
            }
        
            compileOptions {
                sourceCompatibility JavaVersion.VERSION_1_8
                targetCompatibility JavaVersion.VERSION_1_8
            }
        
            kotlinOptions {
                jvmTarget = '1.8'
            }
        }
        
        dependencies {
            implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
            implementation 'androidx.appcompat:appcompat:1.1.0'
            implementation 'com.google.android.material:material:1.1.0'
            implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
            androidTestImplementation 'androidx.test.ext:junit:1.1.1'
            androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
            androidTestImplementation 'androidx.test:runner:1.2.0'
            androidTestImplementation "io.cucumber:cucumber-android:4.4.1"
            androidTestImplementation('com.schibsted.spain:barista:3.3.0') {
                exclude group: 'org.jetbrains.kotlin'
            }
        }
        

        检查我的整个tutorial 以查看它的实际效果。

        【讨论】:

          猜你喜欢
          • 2019-03-10
          • 2011-09-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-13
          • 2015-10-12
          相关资源
          最近更新 更多