CodeLab:Android fundamentals 06.1:Espresso for UI testing

Android fundamentals 06.1:Espresso for UI testing


Tutorial source : Google CodeLab
Date : 2021/04/06

Complete course : 教程目录 (java).

Note : The link in this article requires Google access


1、Welcome

This practical codelab is part of Unit 2: User experience in the Android Developer Fundamentals (Version 2) course. You will get the most value out of this course if you work through the codelabs in sequence:

Note: This course uses the terms “codelab” and “practical” interchangeably.

Introduction

As a developer, it’s important that you test user interactions within your app to make sure that your users don’t encounter unexpected results or have a poor experience with your app.

You can test an app’s user interface (UI) manually by running the app and trying the UI. But for a complex app, you couldn’t cover all the permutations of user interactions within all the app’s functionality. You would also have to repeat these manual tests for different device configurations in an emulator, and on different hardware devices.

When you automate tests of UI interactions, you save time, and your testing is systematic. You can use suites of automated tests to perform all the UI interactions automatically, which makes it easier to run tests for different device configurations. To verify that your app’s UI functions correctly, it’s a good idea to get into the habit of creating UI tests as you code.

Espresso is a testing framework for Android that makes it easy to write reliable UI tests for an app. The framework, which is part of the Android Support Repository, provides APIs for writing UI tests to simulate user interactions within the app—everything from clicking buttons and navigating views to selecting menu items and entering data.

What you should already know

You should be able to:

  • Create and run apps in Android Studio.
  • Check for the Android Support Repository and install it if necessary.
  • Create and edit UI elements using the layout editor and XML.
  • Access UI elements from your code.
  • Add a click handler to a Button.

What you’ll learn

  • How to set up Espresso in your app project.
  • How to write Espresso tests.
  • How to test for user input and check for the correct output.
  • How to find a spinner, click one of its items, and check for the correct output.
  • How to use the Record Espresso Test function in Android Studio.

What you’ll do

  • Modify a project to create Espresso tests.
  • Test the app’s text input and output.
  • Test clicking a spinner item and check its output.
  • Record an Espresso test of a RecyclerView.


2、App overview

Tip: For an introduction to testing Android apps, see Test your app.

In this practical you modify the TwoActivities project from a previous lesson. You set up Espresso in the project for testing, and then you test the app’s functionality.

The TwoActivities app lets a user enter text in a text field and tap the Send button, as shown on the left side of the figure below. In the second Activity, the user views the text they entered, as shown on the right side of the figure below.

在这里插入图片描述



3、Task 1: Set up Espresso in your project

To use Espresso, you must already have the Android Support Repository installed with Android Studio. You may also need to configure Espresso in your project.

In this task you check to see if the repository is installed. If it is not, you will install it.

1.1 Check for the Android Support Repository

  1. Download the TwoActivitiesLifecycle project from an earlier codelab on creating and using an Activity.
  2. Open the project in Android Studio, and choose Tools > Android > SDK Manager.

The Android SDK Default Preferences pane appears.

  1. Click the SDK Tools tab and expand Support Repository.
  2. Look for Android Support Repository in the list.

If Installed appears in the Status column, you’re all set. Click Cancel.

If Not installed or Update Available appears, click the checkbox next to Android Support Repository. A download icon should appear next to the checkbox. Click OK.

  1. Click OK again, and then Finish when the support repository has been installed.

1.2 Configure Espresso for the project

When you start a project for the phone and tablet form factor using API 15: Android 4.0.3 (Ice Cream Sandwich) as the minimum SDK, Android Studio automatically includes the dependencies you need to use Espresso. To execute tests, Espresso and UI Automator use JUnit as their testing framework. JUnit is the most popular and widely-used unit testing framework for Java. Your test class using Espresso or UI Automator should be written as a JUnit 4 test class.

Note: The most current JUnit revision is JUnit 5. However, for the purposes of using Espresso or UI Automator, version 4.12 is recommended.

If you have created your project in a previous version of Android Studio, you may have to add the dependencies and instrumentation yourself. To add the dependencies yourself, follow these steps:

  1. Open the TwoActivities project, or if you prefer, make a copy of the project first and then open the copy.
  2. Open the build.gradle (Module: app) file.
  3. Check if the following is included (along with other dependencies) in the dependencies section:
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 
           'com.android.support.test.espresso:espresso-core:3.0.1'

If the file doesn’t include the above dependency statements, enter them into the dependencies section.

  1. Android Studio also adds the following instrumentation statement to the end of the defaultConfig section of a new project:
testInstrumentationRunner 
                "android.support.test.runner.AndroidJUnitRunner"

If the file doesn’t include the above instrumentation statement, enter it at the end of the defaultConfig section.

Instrumentation is a set of control methods, or hooks, in the Android system. These hooks control an Android component independently of the component’s normal lifecycle. They also control how Android loads apps. Using instrumentation makes it possible for tests to invoke methods in the app, and modify and examine fields in the app, independently of the app’s normal lifecycle.

  1. If you modified the build.gradle (Module: app) file, click the Sync Now link in the notification about Gradle files in top right corner of the window.

1.3 Turn off animations on your test device

To let Android Studio communicate with your device, you must first turn on USB Debugging on your device, as described in the lesson on installing and running apps.

Android phones and tablets display animations when moving between apps and screens. The animations are attractive when using the device, but they slow down performance, and may also cause unexpected results or may lead your test to fail. So it’s a good idea to turn off animations on your physical device. To turn off animations on your test device, tap on the Settings icon on your physical device. Look for Developer Options. Now look for the Drawing section. In this section, turn off the following options:

  • Window animation scale
  • Transition animation scale
  • Animator duration scale

Tip: Instrumenting a system, for example executing unit tests, can alter the timing of some functions. For this reason, keep unit testing and debugging separate:

  • Unit testing uses an API based Espresso framework with hooks for instrumentation.
  • Debugging uses breakpoints and other methods in the coding statements within your app’s code, as described in the lesson on debugging.


4、Task 2: Test for Activity switching and text input

You write Espresso tests based on what a user might do while interacting with your app. The Espresso tests are classes that are separate from your app’s code. You can create as many tests as you need in order to interact with the View elements in your UI that you want to test.

The Espresso test is like a robot that must be told what to do. It must find the View you want it to find on the screen, and it must interact with it, such as clicking the View, and checking the contents of the View. If it fails to do any of these things properly, or if the result is not what you expected, the test fails.

With Espresso, you create what is essentially a script of actions to take on each View and check against expected results. The key concepts are locating and then interacting with UI elements. These are the basic steps:

  1. Match a View: Find a View.
  2. Perform an action: Perform a click or other action that triggers an event with the View.
  3. Assert and verify the result: Check the state of the View to see if it reflects the expected state or behavior defined by the assertion.

Hamcrest (an anagram of “matchers”) is a framework that assists writing software tests in Java. To create a test, you create a

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值