Android 单元测试之Espresso - Google官方UI测试框架

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);
            }
        };
    }
}

然后,运行,发现测试通过

其他

源码Demo
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

氦客

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值