Android 单元测试之Espresso - Google官方UI测试框架
Android 单元测试之JUnit和Mockito
Android 单元测试之Roboletric 环境配置
Android 单元测试之Roboletric的简单使用
Android 单元测试之Roboletric RxJava、Retrofit、访问真实网络、虚拟服务器
Android 单元测试之Espresso - Google官方UI测试框架
Espresso是Google官方推出的Instrumentation UI测试框架,在API支持方面有着天然的优势,在推出后很大程度上将替代Robotium。
Espresso官方文档
配置
android {
...
defaultConfig {
...
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
packagingOptions {
exclude 'LICENSE.txt'
}
}
dependencies {
...
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2') {
exclude module: 'support-annotations'
exclude module: 'design'
exclude module: 'recyclerview-v7'
exclude module: 'support-v4'
}
androidTestCompile ('com.android.support.test.espresso:espresso-idling-resource:2.2.2') {
exclude module: 'support-annotations'
}
compile 'com.android.support:design:24.1.1'
}
构建时,可能会出现annotations 相关报错,加上下面的就好
android {
configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:23.0.1'
}
}
原因是目前 Espresso 对Android 部分代码只支持到 23.0.1 而我们项目中有些android版本为23.1.1,所以出现了报错
最简单的使用
新建布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="@+id/greet_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="greet"
android:text="@string/greet" />
<TextView
android:id="@+id/greeting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28sp" />
</LinearLayout>
新建Activity
public class MainActivity extends AppCompatActivity {
private TextView greetingView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle(R.string.app_name);
greetingView = (TextView) findViewById(R.id.greeting);
}
public void greet(View v) {
greetingView.setText(R.string.hello);
}
}
新建单元测试
在androidTest中新建
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> activityRele = new ActivityTestRule<>(MainActivity.class);
@Test
public void greet() {
onView(withId(R.id.greeting)).check(matches(withText("")));
onView(withId(R.id.greet_button)).check(matches(withText(R.string.greet))).perform(click());
onView(withId(R.id.greeting)).check(matches(withText(R.string.hello)));
}
@Test
public void toolbarTitle() {
CharSequence title = InstrumentationRegistry.getTargetContext().getString(R.string.app_name);
matchToolbarTitle(title);
}
private static ViewInteraction matchToolbarTitle(CharSequence title) {
return onView(isAssignableFrom(Toolbar.class))
.check(matches(withToolbarTitle(is(title))));
}
private static Matcher<Object> withToolbarTitle(final Matcher<CharSequence> textMatcher) {
return new BoundedMatcher<Object, Toolbar>(Toolbar.class) {
@Override
public boolean matchesSafely(Toolbar toolbar) {
return textMatcher.matches(toolbar.getTitle());
}
@Override
public void describeTo(Description description) {
description.appendText("with toolbar title: ");
textMatcher.describeTo(description);
}
};
}
}
然后,运行,发现测试通过