官网地址java
Espresso是Google官方的一个针对Android UI测试的库,能够自动化的进行UI测试。android
Espresso能够验证View的可见性,文字显示是否正确,图片是否正确,位置等等,相对于人工测试,Espresso覆盖更全,测试速度更快。web
UI测试分为三个部分:ViewMatcher、ViewAction、ViewAssertion。app
通常的测试流程就是按照上面图示的步骤来进行,首先匹配到UI组件,而后执行一些操做,好比click(),而后执行断言判断。其中每一个部分包括不少个方法,官方有一个图:ide
能够看到每一个步骤下面有不少个方法,在写测试用例的时候均可以使用。单元测试
对于普通的UI组件测试,在以前的Junit的测试中说,全部UI测试相关的都在androidTest文件夹下,看下一个简单的例子:测试
@RunWith(AndroidJUnit4::class)
class MainActivityTest {
@get:Rule
public val activity = ActivityTestRule(MainActivity::class.java)
@Test
fun onViewClicked() {
onView(withId(R.id.tv_content)).check(matches(not(isDisplayed())))
onView(withId(R.id.btn_change)).check(matches(withText("change"))).perform(click())
onView(withId(R.id.tv_content)).check(matches(withText("content"))).check(matches(isDisplayed()))
}
}
复制代码
能够看出,测试UI的流程就是按照上面的三个步骤来进行的。gradle
引入:ui
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'
复制代码
在一些场景下,可能须要测试Intent的跳转,可是可能并不须要真正去执行这个跳转的操做,实际上只须要验证一下这个跳转的intent是否发送成功就能够了。Espresso提供了两个方法:intended
和 indending
,这两个方法分别能够当作是Mockito中的verify()和when (),通常状况下,若是跳转不须要返回值,就使用 intended
,若是跳转须要返回值,则用 indending
模拟一个返回值。看一个简单的例子:url
//若是须要测试Intent,这里的Rule须要更换成IntentTestRule
@get:Rule
public val intentRule = IntentsTestRule(MainActivity::class.java)
private val PACKAGE_NAME = "com.example.myapplication"
@Test
fun onIntent(){
onView(withId(R.id.btn_intent)).perform(click())
//点击btn跳转到SecondActivity, 验证intent中是否包含有SecondActivity组件,以及目标package是否为指定的package。
intended(allOf(hasComponent(hasShortClassName(".SecondActivity")), toPackage(PACKAGE_NAME)))
}
复制代码
若是使用的是startActivityforResult的话,须要返回值,能够按照以下的写法:
val resultIntent = Intent()
resultIntent.putExtra("result", "OK")
val result = Instrumentation.ActivityResult(Activity.RESULT_OK, resultIntent)
intending(allOf(hasComponent(hasShortClassName(".SecondActivity")), toPackage(PACKAGE_NAME))).respondWith(result)
复制代码
上面的代码就是利用intending
对目标Intent构造了一个返回值,和 when().thenReturn()
有点相似。
引入:
androidTestImplementation 'androidx.test.espresso:espresso-web:3.1.0'
复制代码
除了对于一些普通的控件进行UI测试以外,Espresso还能够对WebView进行测试,而且能够获取web页中的element,对其进行一些Action、或者获取当前加载的url、也能够检查某些控件中是否包含有某些字段,下面是一个简单的例子:
@Test
fun onLoadUrl(){
onView(withId(R.id.btn_start_webview)).perform(click())
//onIdle()
//检测当前加载的url中是否包含bing
onWebView().check(webMatches(getCurrentUrl(), containsString("bing")))
}
复制代码
还能够检测WebView中元素,而且进行断言判断:
onWebView()
.withElement(findElement(Locator.ID, "teacher"))
.withContextualElement(findElement(Locator.ID, "person_name"))
.check(webMatches(getText(), containsString("Socrates")))
复制代码
检测teacher.person_name是否包含有Socrates。
也能够对WebView中的元素进行操做:
onWebView()
.withElement(findElement(Locator.ID, "teacher"))
.perform(webClick())
复制代码
在一些状况下,可能系统提供的Matcher并不能知足需求,这时候也能够经过自定义Matcher来实现:
fun textViewTextColorMatcher(matcherColor: Int): Matcher<View> {
return object: BoundedMatcher<View, TextView>(TextView::class.java){
override fun describeTo(description: Description?) {
description?.appendText("with test color: $matcherColor")
}
override fun matchesSafely(item: TextView?): Boolean {
return matcherColor == item?.currentTextColor
}
}
}
复制代码
上述代码自定义了一个TextView的textColor的匹配器,describeTo是当匹配失败的时候的提示,matchesSafely是主要的匹配逻辑。
而后就能够经过如下方式来使用自定义的匹配器了。
onView(withId(R.id.search_action_button)).check(matches(textViewTextColorMatcher(TEXT_BTN_COLOR_DISABLED)))
复制代码
当使用gralde/app/verification/test 编译的时候,会运行全部的测试类(包括全部的module),而且在对应的build/reports/tests/下面生成一个测试报告(也能够经过运行命令 ./gradlew test
)。能够经过这个测试报告来查看到底有多少测试类经过,多少失败,而后针对性的检查问题。下图就是跑了test以后生成的报告:
下图是集成到了demo里的jacoco输出的覆盖率报告:
能够看到有覆盖率的分析,包括代码覆盖率、分支覆盖率等等。