Espresso 不依赖源代码的测试

之前一段时间通过同步更新开发的代码进行Espresso的测试,发现经常因为build.gradle的更新,进行会出现一些冲突的问题,当然也是个人能力的问题,总觉得经常花费时间在解决这些问题上有些不值当,所以Espresso的测试如果能够脱离源代码只需要依赖apk的话就更好了。

Espresso也是基于Instrumentation实现的,所以我们可以尝试通过robotium的方式来解决这个问题。
具体可以参考Android Espresso how to write tests using apk

@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

  private static final String CLASSNAME = "com.rex.binderapps.RecorderActivity";

  private static Class<? extends Activity> activityClass;
  static {
    try {
      activityClass = (Class<? extends Activity>) Class.forName(CLASSNAME);
    } catch (ClassNotFoundException e) {
      throw new RuntimeException(e);
    }
  }

  @Rule 
  public final ActivityTestRule<?> activityRule 
      = new ActivityTestRule<>(activityClass);

  @Test
  public void launchMain() {

  }
}
  1. 首推还是使用Android Studio 进行开发,毕竟这个是google的。并且在Android Studio 2.2的版本里面推出了Espresso 录制器(下来分享下)虽然个人感觉还是不太好用,但是至少通过它来了解下Espresso 的基本用法。
  2. 我们在Android Studio 中新建一个包名与待测试APK一样的android工程。
  3. 配置我们的build.gradle。下来是我的配置内容
apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    packagingOptions {
        exclude 'LICENSE.txt'
    }
    defaultConfig {
        applicationId "com.seewo.easicare.demo"
        minSdkVersion 18
        targetSdkVersion 23
        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:23.0.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.espresso:espresso-intents:2.2.1'
    androidTestCompile ('com.android.support.test.espresso:espresso-idling-resource:2.2') {
        exclude module: 'support-annotations'
    }
    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.0') {
        exclude group: 'com.android.support', module: 'appcompat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude module: 'recyclerview-v7'
    }

    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
}
  1. 编写我们的测试代码,代码就跟上面的测试代码类似。PS : 这里可能害有一些难点的地方,因为我们Espresso的api中可以通过id查找元素,但是我们是没有源码的 所以我们不能够通过类似于onView(withId(R.id.button)).perform(click()); 这种方式去获取到元素。 可以参考Espresso How to access views without using R.id.viewid as we do in robotium?
  2. 完成我们的代码编写后,当然就是运行我们的测试程序拉,这里我们通过gralde 编译出测试的apk ./gradlew assembleAndroidTest
  3. 安装我们的测试apk adb install -r app-debug-androidTest-unaligned.apk
  4. 运行我们的测试apk adb shell am instrument -w com.seewo.easicare.demo.test/android.support.test.runner.AndroidJUnitRunner 当然其他的运行方式可以参考这里
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值