Espress介绍
谷歌13年的时候开源了espress,谷歌的思路是,等到它足够成熟和稳定以后,将其迁移到Android SDK中,以此可见对他的重视。Google使用Espresso测试了他们自己的超过30个应用程序,包括G+、Maps和Drive。下面对Espresso做一个基本介绍
Espresso测试是非常容易实现的,它由三部分组成:
ViewMachers:寻找用来测试的View。
ViewActions:发送交互事件。
ViewAssertions:检验测试结果。
用法
先直接放用法,以下用例的执行效果是直接打开MainActivity,执行showMain中的用例
onView(ViewMachers)
.preform(ViewActions)
.check(ViewAssertions)
@Rule
public IntentsTestRule mainActivityIntentsTestRule = new IntentsTestRule(MainActivity.class);
@Test
public void showMain(){
//是否选中
onView(withId(R.id.tv_username)).check(matches(isDisplayed()));
//测试点击
onView(withId(R.id.tv_username)).perform(click());
//根据文本内容匹配
String username ="HELLO Brian123!";
onView(withId(R.id.tv_username)).check(matches(withText(username)));
}
Espresso 备忘录
方便你在开发过程中快速查阅。它包含了大部分可用的Matchers,ViewActions, 和ViewAssertions。pdf 格式的离线版本在此:espersso-cheat-sheet-2.1.0.pdf
搭建步骤
项目目录/build.gradle中的ext版本号:
// Define versions in a single place
ext {
// Sdk and tools
minSdkVersion =15
targetSdkVersion =22
compileSdkVersion =23
buildToolsVersion ='23.0.3'
//app Code
versionCode =1
versionName ="1.0"
// App dependencies
supportLibraryVersion ='23.4.0'
junitVersion ='4.12'
mockitoVersion ='1.10.19'
powerMockito ='1.6.2'
hamcrestVersion ='1.3'
runnerVersion ='0.4.1'
rulesVersion ='0.4.1'
espressoVersion ='2.2.1'
}
然后在android studio的项目目录下/app/build.gradle中配置
testCompile"junit:junit:$rootProject.ext.junitVersion"
androidTestCompile"junit:junit:$rootProject.ext.junitVersion"
// Android Testing Support Library's runner and rules
androidTestCompile"com.android.support.test:runner:$rootProject.ext.runnerVersion"
androidTestCompile"com.android.support.test:rules:$rootProject.ext.runnerVersion"
// Resolve conflicts between main and test APK:
androidTestCompile"com.android.support:support-annotations:$rootProject.supportLibraryVersion"
androidTestCompile"com.android.support:support-v4:$rootProject.supportLibraryVersion"
androidTestCompile"com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"
// Espresso UI Testing
androidTestCompile"com.android.support.test.espresso:espresso-core:$rootProject.espressoVersion"
androidTestCompile ("com.android.support.test.espresso:espresso-contrib:$rootProject.espressoVersion")
androidTestCompile"com.android.support.test.espresso:espresso-intents:$rootProject.espressoVersion"
compile"com.android.support.test.espresso:espresso-idling-resource:$rootProject.espressoVersion"
// Android Testing Support Library's runner and rules
androidTestCompile"com.android.support.test:runner:$rootProject.ext.runnerVersion"
androidTestCompile"com.android.support.test:rules:$rootProject.ext.runnerVersion"
Espresso和UIAutomator的区别
Espresso是个功能强大、效率很高的Android自动化测试框架,但是他有一个重要的局限,你只能在被测试app的Context中操作。
所以应用的推送、从另一个应用程序进入、处理通知栏消息等功能无法在Espresso中使用,需要混合UIAutomator使用
其他资料参考
http://www.jianshu.com/p/ef4ad5424784 官方文档翻译
http://www.w2bc.com/article/40324?from=extend
http://axiu.me/coding/espresso-introduction/
https://github.com/googlesamples/android-testing 谷歌自动化测试demo
网友评论