Android单元测试总结

一、单元测试:
1、Android单元测试的利器:Juint(一):Juint的介绍和基础使用
2、Android单元测试的利器:Juint(二):Juint的生命周期
3、Android单元测试的利器:Juint(三):Juint的详细用法(上)

二、Android测试(Instrumentation测试)
使用Android Studio 3.0新建工程,使用Android视图在java目录下会有三个目录,一个是android的source,一个带(test),一个带(android Test),带(android Test)的是用于Instrumentation测试的。

这里写图片描述

1、引用库:

  android {
    defaultConfig {
         ……
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        ……
    }

 }

dependencies {
    ……
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestCompile 'com.android.support.test:rules:1.0.2'
    ……
}

2、测试:

<RelativeLayout 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" tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:text="Hello" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:hint="Enter your name here"
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Say hello!"
        android:layout_below="@+id/editText"
        android:onClick="sayHello"/>
</RelativeLayout>
@RunWith(AndroidJUnit4.class)
public class MainActivityInstrumentedTest {


    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
            MainActivity.class);

    private static final String STRING_TO_BE_TYPED = "Peter";
    @Test
    public void sayHello(){
        onView(withId(R.id.editText)).perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard()); //line 1

        onView(withText("Say hello!")).perform(click()); //line 2

        String expectedText = "Hello, " + STRING_TO_BE_TYPED + "!";
        onView(withId(R.id.textView)).check(matches(withText(expectedText))); //line 3
    }
}

3、相关api:
https://blog.csdn.net/qiang_xi/article/details/52933593

更新中……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值