Android - 单元测试,espresso的使用

栏目: 编程工具 · 发布时间: 7年前

内容简介:For testing Android apps, you typically create these types of automated unit tests:对于Android Studio3来说,新建项目后一般已经引入了这些依赖包。检查并指定

For testing Android apps, you typically create these types of automated unit tests:

  • Local tests: Unit tests that run on your local machine only. These tests are compiled to run locally on the Java Virtual Machine (JVM) to minimize execution time. Use this approach to run unit tests that have no dependencies on the Android framework or have dependencies that can be filled by using mock objects.
  • Instrumented tests: Unit tests that run on an Android device or emulator. These tests have access to instrumentation information, such as the [Context](https://developer.android.com/reference/android/content/Context.html) for the app under test. Use this approach to run unit tests that have Android dependencies which cannot be easily filled by using mock objects.

使用 junit + espresso 进行测试

引入依赖包

对于Android Studio3来说,新建项目后一般已经引入了这些依赖包。

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

检查并指定 testInstrumentationRunner

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

否则可能会报错: junit.framework.AssertionFailedError: No tests found xxx

编写单元测试代码

espresso 功能点

记录一些常用的基础功能。

大多数都在 android.support.test.espresso 包里。

  • Espresso.onView 获取一个可与View交互的对象 ViewInteraction
  • ViewMatchers.withId 获取 Matcher

代码示例

以aboutView项目中的单元测试代码为例,针对 AddDataActivity

import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log;
import android.view.View;

import com.rust.aboutview.contactview.AddDataActivity;

import org.hamcrest.Matcher;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isClickable;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.isEnabled;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.not;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class UITest1 {
    private static final String TAG = "rustAppUnitTest";

    @Rule
    public ActivityTestRule<AddDataActivity> mAddData = new ActivityTestRule<>(AddDataActivity.class); // 必须是public

    @Before
    public void prepareData() {
        System.out.println("做一些准备工作,比如预装数据");
        Log.d(TAG, "prepareData.");
    }

    /// 检查初始状态
    @Test
    public void checkDefStatus() {
        onView(withText("Add Data")).check(matches(isDisplayed()));

        onView(withId(R.id.done_add_data_button)).check(matches(not(isEnabled())));
        onView(withId(R.id.done_add_data_button)).check(matches((isClickable())));
    }

    @Test
    public void addPersonData() {
        Log.d(TAG, "addPersonData test starts.");
        Matcher<View> confirmBtn = withId(R.id.done_add_data_button);
        Matcher<View> nameEt = withId(R.id.add_name);
        Matcher<View> phoneEt = withId(R.id.add_phone);
        Matcher<View> emailEt = withId(R.id.add_email);

        onView(nameEt).perform(replaceText("Test Name " + System.currentTimeMillis()));
        onView(confirmBtn).check(matches(isEnabled())); // 输入用户名后的状态
        onView(phoneEt).perform(replaceText("13312345678"));
        onView(emailEt).perform(replaceText("xx@yy.com"));
        onView(confirmBtn).perform(click());
    }
}

这个测试执行后,可以正常打开app看看。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

About Face 4: 交互设计精髓

About Face 4: 交互设计精髓

[美] 艾伦·库伯、[美] 罗伯特·莱曼、[美] 戴维·克罗宁、[美] 克里斯托弗·诺埃塞尔 / 倪卫国、刘松涛、杭敏、薛菲 / 电子工出版社 / 2015-10 / 118.00元

《About Face 4: 交互设计精髓》是《About Face 3:交互设计精髓》的升级版,此次升级把全书的结构重组优化,更加精练和易用;更新了一些适合当下时代的术语和实例,文字全部重新编译,更加清晰易读;增加了更多目标导向设计过程的细节,更新了现行实践,重点增加 移动和触屏平台交互设计,其实《About Face 4: 交互设计精髓》多数内容适用于多种平台。 《About F......一起来看看 《About Face 4: 交互设计精髓》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

SHA 加密
SHA 加密

SHA 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换